deserialization

How to deserialize an object with PyYAML using safe_load?

£可爱£侵袭症+ 提交于 2019-12-21 04:51:33
问题 Having a snippet like this: import yaml class User(object): def __init__(self, name, surname): self.name= name self.surname= surname user = User('spam', 'eggs') serialized_user = yaml.dump(user) #Network deserialized_user = yaml.load(serialized_user) print "name: %s, sname: %s" % (deserialized_user.name, deserialized_user.surname) Yaml docs says that it is not safe to call yaml.load with any data received from an untrusted source; so, what should I modify to my snippet\class to use safe_load

Ignore null fields when DEserializing JSON with Gson or Jackson

孤街浪徒 提交于 2019-12-21 04:22:35
问题 I know there's lots of questions about skipping fields with a null value when serializing objects to JSON. I want to skip / ignore fields with null values when deserializing JSON to an object. Consider the class public class User { Long id = 42L; String name = "John"; } and the JSON string {"id":1,"name":null} When doing User user = gson.fromJson(json, User.class) I want user.id to be '1' and user.name to be 'John'. Is this possible with either Gson or Jackson in a general fashion (without

How to deserialize XML to JavaScript/TypeScript?

大城市里の小女人 提交于 2019-12-20 20:29:31
问题 I get a XML structure that contains information about an Appointment. My code: function AppointmentCallback(appointment : any) { } I want to convert this in an object in JavaScript or TypeScript(preferred). In jQuery there's only the parseXML method which makes the opposite from what I need. Is there any library that makes this possible? Thanks in advance! 回答1: cxml can parse XML into JSON and also fire handlers during parsing to process a file as it loads. You can compile .xsd schema files

How to serialize an array and deserialize back

纵饮孤独 提交于 2019-12-20 19:56:12
问题 How do I serialize an array and deserialize it back from a string? I tried the following code, but it doesn't really return the original array of integers but does for the array of strings. x = [1,2,3].join(',') # maybe this is not the correct way to serialize to string? => '1,2,3' x = x.split(',') => [ '1', '2', '3' ] Is there a way to get it back to integers without having the .collect{ |x| x.to_i } ? 回答1: The standard way is with Marshal : x = Marshal.dump([1, 2, 3]) #=> "\x04\b[\bi\x06i

JSON deserialization to inherited types

偶尔善良 提交于 2019-12-20 18:06:39
问题 I have a data table in my database where I store various settings. Since they are of any type (even complex object graphs) I decided to store their values as serialized JSON strings. Let's say that I serialized a List<ItemBase> . Serialized string looks just fine. But the problem is that list items are of various types that are inherited from ItemBase (which may as well be abstract for what I care). Question Which (de)serialization class/library should I use so my JSON strings will be

What are @JsonTypeInfo and @JsonSubTypes used for in jackson

穿精又带淫゛_ 提交于 2019-12-20 17:25:37
问题 What are @JsonTypeInfo and @JsonSubTypes annotations using for in jackson ? public class Lion extends Animal { private String name; @JsonCreator public Lion(@JsonProperty("name") String name) { this.name = name; } public String getName() { return name; } public String getSound() { return "Roar"; } public String getType() { return "carnivorous"; } public boolean isEndangered() { return true; } @Override public String toString() { return "Lion [name=" + name + ", getName()=" + getName() + ",

Jackson - Deserialising JSON string - TypeReference vs TypeFactory.constructCollectionType

a 夏天 提交于 2019-12-20 09:32:36
问题 To deserialise JSON string to a list of class, different ways listed at StackOverflow question Type 1 (docs link): List<SomeClass> someClassList = mapper.readValue(jsonString, typeFactory.constructCollectionType(List.class, SomeClass.class)); Type 2 (docs link): List<SomeClass> list = mapper.readValue(jsonString, new TypeReference<List<SomeClass>>() { }); Though both 2 types above do the job, what is the difference between these implementations ? 回答1: After constructing JavaType , both call

Deserialize JSON to classes

我怕爱的太早我们不能终老 提交于 2019-12-20 09:18:41
问题 Server returns such part of JSON: {"condition": { "or": [ { "and": [ { "operand": "a", "operator": "==", "value": "true" }, { "not": { "operand": "b", "operator": "==", "value": "true" } } ] }, { "and": [ { "operand": "b", "operator": "==", "value": "true" }, { "not": { "operand": "a", "operator": "==", "value": "true" } } ] } ] }} I wrote next classes hierarchy: public interface Condition {} public class Expression implements Condition { public Expression(String operator, String operand,

JSON Jackson deserialization multiple keys into same field

不想你离开。 提交于 2019-12-20 06:34:15
问题 I am trying to convert JSON into POJO. I have worked with Jackson to convert standard JSON file. In this particular case, I would like to overwrite the key value to "default" class/variable. In this case, there are multiple key value to be replaced (ie. hundreds, and the key values to be replaced are unknown). Is this possible? I thought of storing it into Map, then iterate and store each into POJO, but wondering if there is different option, since I am not that familiar with storing JSON to

Deserialisation issue - java.io.StreamCorruptedException: invalid type code: 00

↘锁芯ラ 提交于 2019-12-20 06:27:41
问题 I'm writing a java file-transfer app, and i have some troubles with deserialisation myself-defined class Message from Datagramms. Other topics at StackOverflow has similar issues, but i didn't found something helpful from there, so i'm sorry in advance if i missed it. so, class Message is: import java.io.Serializable; public class Message implements Serializable { private static final long serialVersionUID = 1L; private int segmentID; private byte[] packet; private int bytesToWrite; public