Skip to main content
added 355 characters in body
Source Link
esskar
  • 11k
  • 3
  • 38
  • 58

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

EDIT2: Here is an example of what i am trying to do

public enum Foo { A, B, C } public enum Bar { A, B, C } public class Misc { public Foo Foo { get; set; } } var dict = new Dictionary<string, object>(); dict.Add("a", Foo.A); dict.Add("b", Bar.B); dict.Add("c", new Misc()); // serialize dict to a string s // deserialize s to a Dictionary<string, object> dict2 Assert.AreEqual(Foo.A, dict2["a"]); Assert.AreEqual(Bar.B, dict2["b"]); 

Important: i cannot control dict; it is actually a custom type that is derived from Dictionary<string, object>: I just have to make sure that all keys and values deserialized are from the same type when deserialized, so that no casting is needed. And again, i do not have to use JSON; maybe there is some other serializer that can handle the job!?

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

EDIT2: Here is an example of what i am trying to do

public enum Foo { A, B, C } public enum Bar { A, B, C } public class Misc { public Foo Foo { get; set; } } var dict = new Dictionary<string, object>(); dict.Add("a", Foo.A); dict.Add("b", Bar.B); dict.Add("c", new Misc()); // serialize dict to a string s // deserialize s to a Dictionary<string, object> dict2 Assert.AreEqual(Foo.A, dict2["a"]); Assert.AreEqual(Bar.B, dict2["b"]); 

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

EDIT2: Here is an example of what i am trying to do

public enum Foo { A, B, C } public enum Bar { A, B, C } public class Misc { public Foo Foo { get; set; } } var dict = new Dictionary<string, object>(); dict.Add("a", Foo.A); dict.Add("b", Bar.B); dict.Add("c", new Misc()); // serialize dict to a string s // deserialize s to a Dictionary<string, object> dict2 Assert.AreEqual(Foo.A, dict2["a"]); Assert.AreEqual(Bar.B, dict2["b"]); 

Important: i cannot control dict; it is actually a custom type that is derived from Dictionary<string, object>: I just have to make sure that all keys and values deserialized are from the same type when deserialized, so that no casting is needed. And again, i do not have to use JSON; maybe there is some other serializer that can handle the job!?

added an brief example
Source Link
esskar
  • 11k
  • 3
  • 38
  • 58

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

EDIT2: Here is an example of what i am trying to do

public enum Foo { A, B, C } public enum Bar { A, B, C } public class Misc { public Foo Foo { get; set; } } var dict = new Dictionary<string, object>(); dict.Add("a", Foo.A); dict.Add("b", Bar.B); dict.Add("c", new Misc()); // serialize dict to a string s // deserialize s to a Dictionary<string, object> dict2 Assert.AreEqual(Foo.A, dict2["a"]); Assert.AreEqual(Bar.B, dict2["b"]); 

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

EDIT2: Here is an example of what i am trying to do

public enum Foo { A, B, C } public enum Bar { A, B, C } public class Misc { public Foo Foo { get; set; } } var dict = new Dictionary<string, object>(); dict.Add("a", Foo.A); dict.Add("b", Bar.B); dict.Add("c", new Misc()); // serialize dict to a string s // deserialize s to a Dictionary<string, object> dict2 Assert.AreEqual(Foo.A, dict2["a"]); Assert.AreEqual(Bar.B, dict2["b"]); 
edited to explain why StringEnumConverter is not an option
Source Link
esskar
  • 11k
  • 3
  • 38
  • 58

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

I am trying to serialize/deserialize a Dictionary<string, object> in C#. Object can be anything that is serializable.

Json.NET almost works, but if a value within the dictionary is an enum, the deserialization is not correct, as it gets deserialized as a long. TypeNameHandling.All does not make any difference.

Is there any other fast solution to serialization library. The result does not have to be JSON, but must be text.

I also have no influence on the data that is passed to the dictionary. I just have to serialize and deserialize anything that comes into my way.

EDIT: StringEnumConverter does not help. The data gets converted back to Dictionary<string, object>, so the deserializer does not know that the serialized value is an enum. It treats it like an object, with StringEnumConverter it remains a string when deserialized; it gets deserialized as a long without the converter. JSON.NET does not preserve the enum.

The solution i want to provide is an implementation of an existing interface that gets injected into an existing solution that i cannot change.

Source Link
esskar
  • 11k
  • 3
  • 38
  • 58
Loading