deserialization

Deserialize Json string to Enum C#

假如想象 提交于 2020-06-08 20:27:29
问题 I am writing a test on a custom version of stringEnumConverter. But my test keeps throwing when I deserialize. I searched over stack overflow, but could not find what I did wrong. Following is a sample of what I'm doing: namespace ConsoleApp2 { [Flags] [JsonConverter(typeof(StringEnumConverter))] enum TestEnum { none = 0, obj1 = 1, obj2 = 2 } class Program { static void Main(string[] args) { var jsonString = "{none}"; var deserializedObject = JsonConvert.DeserializeObject<TestEnum>(jsonString

Recursive generic function with type passed as a parameter

旧巷老猫 提交于 2020-06-01 05:22:07
问题 I want to have a recursive generic function, but I cannot use type passed as an argument in generic function invocation, cause of 'memberType' refers to a value, but is being used as a type here. Is there a way to pass memberType to generic method invocation? Example: class Packet { header: Header body: Body static MEMBERS = [ ['header', Header, 0, 6], ['body', Body, 6, 10], ] } class Header { size: number ttl: number static MEMBERS = [ ['size', 'number', 0, 2], ['ttl', 'number', 2, 3], ] }

How to read and write an object to a text file in java?

时光怂恿深爱的人放手 提交于 2020-05-27 05:19:27
问题 I have an array of objects and I want to write them in a text file. So that I can later read the objects back in an array. How should I do it? Using Serialization. Deserialization is not working: public static void readdata(){ ObjectInputStream input = null; try { input = new ObjectInputStream(new FileInputStream("myfile.txt")); // getting end of file exception here } catch (FileNotFoundException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } try { array =

How to serialize/deserialize Kotlin sealed class?

混江龙づ霸主 提交于 2020-05-26 12:09:31
问题 I have a following sealed class: sealed class ViewModel { data class Loaded(val value : String) : ViewModel() object Loading : ViewModel() } How can I serialize/deserialize instances of the ViewModel class, let's say to/from JSON format? I've tried to use Genson serializer/deserializer library - it can handle Kotlin data classes, it's also possible to support polymorphic types (eg. using some metadata to specify concrete types). However, the library fails on Kotlin object types, as these are

Deserialize an XML string based on class file generated with XSD

一世执手 提交于 2020-05-17 08:11:01
问题 I am trying to deserialize an XML answer based on classes created by XSD schema but it always returns null. The XML file has this format <?xml version="1.0" encoding="utf-16"?> <Responses xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Response> <inv_number>1</inv_number> <StatusCode>Success</StatusCode> <Uid>271D95D28716B37A330A5A476AE530206828B103</Uid> <Mark>1000000912965</Mark> </Response> <Response> <inv_number>2</inv_number>

Reading large XML files with C#

元气小坏坏 提交于 2020-05-16 21:56:02
问题 I would like to know how can I read a XML file from my desktop and put it into a string? Here is my XML: <smallusers> <user id="1"> <name>John</name> <motto>I am john, who are you?</motto> </user> <user id="2"> <name>Peter</name> <motto>Hello everyone!</motto> </user> </smallusers> <bigusers> <user id="3"> <name>Barry</name> <motto>Earth is awesome</motto> </user> </bigusers> I want to store each user, but still detect if their small or big, is there a way to do this? Before you downrate this

Reading large XML files with C#

狂风中的少年 提交于 2020-05-16 21:54:07
问题 I would like to know how can I read a XML file from my desktop and put it into a string? Here is my XML: <smallusers> <user id="1"> <name>John</name> <motto>I am john, who are you?</motto> </user> <user id="2"> <name>Peter</name> <motto>Hello everyone!</motto> </user> </smallusers> <bigusers> <user id="3"> <name>Barry</name> <motto>Earth is awesome</motto> </user> </bigusers> I want to store each user, but still detect if their small or big, is there a way to do this? Before you downrate this

symfony deserialize nested objects

你。 提交于 2020-05-15 09:21:26
问题 I have used the Symfony serializer to serialize my Recherche object. In a Recherche object, I have sub objects : Categorie and Lieu . When I deserialize my Recherche object, all the sub objects are transformed in arrays. I would like them to be objects again. This is how I have serialized my object: $encoders = array(new JsonEncoder()); $normalizer = new ObjectNormalizer(); $normalizer->setIgnoredAttributes(array('parent', 'enfants')); $normalizer->setCircularReferenceHandler(function (

jackson xml deserialize inline array

穿精又带淫゛_ 提交于 2020-05-11 04:34:11
问题 How to deserialize such strange XML. In my opinion, the props-entity is missing (around the props), but I can't change the source of this XML (a web service). <parents> <parent name="first"> <description><![CDATA[Description for the first-Entity]]></description> <prop name="level"> <value><![CDATA[1]]></value> </prop> <prop name="enabled"> <value><![CDATA[true]]></value> </prop> <prop name="version"> <value><![CDATA[1.0-beta3]]></value> </prop> </parent> <parent name="second">...</parent> ...

Jackson JSON Deserialization: array elements in each line

守給你的承諾、 提交于 2020-05-10 07:41:46
问题 I am using Jackson and would like to pretty-print JSON such that each element in arrays goes to each line, like: { "foo" : "bar", "blah" : [ 1, 2, 3 ] } Setting SerializationFeature.INDENT_OUTPUT true only inserts newline characters for object fields, not array elements, printing the object in this way instead: { "foo" : "bar", "blah" : [1, 2, 3] } Does anyone know how to achieve this? Thanks! 回答1: You could extend the DefaultPrettyPrinter and override the methods beforeArrayValues(…) and