Decoding large JSON files in C# - c#

While downloading auction data from blizzard (a 6 MB JSON file), on decode, I was able to trace the error to this:
"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.\r\nParameter name: input"
How can I change the maxJsonLength to say 20 MB (as some of the JSON I parse can be that large) in the following class?
public sealed class DynamicJsonConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IEnumerable<Type> SupportedTypes
{
get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
}
#region Nested type: DynamicJsonObject
private sealed class DynamicJsonObject : DynamicObject
{
private readonly IDictionary<string, object> _dictionary;
public DynamicJsonObject(IDictionary<string, object> dictionary)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
_dictionary = dictionary;
}
public override string ToString()
{
var sb = new StringBuilder("{");
ToString(sb);
return sb.ToString();
}
private void ToString(StringBuilder sb)
{
var firstInDictionary = true;
foreach (var pair in _dictionary)
{
if (!firstInDictionary)
sb.Append(",");
firstInDictionary = false;
var value = pair.Value;
var name = pair.Key;
if (value is string)
{
sb.AppendFormat("{0}:\"{1}\"", name, value);
}
else if (value is IDictionary<string, object>)
{
new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);
}
else if (value is ArrayList)
{
sb.Append(name + ":[");
var firstInArray = true;
foreach (var arrayValue in (ArrayList)value)
{
if (!firstInArray)
sb.Append(",");
firstInArray = false;
if (arrayValue is IDictionary<string, object>)
new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);
else if (arrayValue is string)
sb.AppendFormat("\"{0}\"", arrayValue);
else
sb.AppendFormat("{0}", arrayValue);
}
sb.Append("]");
}
else
{
sb.AppendFormat("{0}:{1}", name, value);
}
}
sb.Append("}");
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (!_dictionary.TryGetValue(binder.Name, out result))
{
// Return null to avoid exception. The caller can check for null this way...
result = null;
return true;
}
result = WrapResultObject(result);
return true;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
if (indexes.Length == 1 && indexes[0] != null)
{
if (!_dictionary.TryGetValue(indexes[0].ToString(), out result))
{
// Return null to avoid exception. The caller can check for null this way...
result = null;
return true;
}
result = WrapResultObject(result);
return true;
}
return base.TryGetIndex(binder, indexes, out result);
}
private static object WrapResultObject(object result)
{
var dictionary = result as IDictionary<string, object>;
if (dictionary != null)
return new DynamicJsonObject(dictionary);
var arrayList = result as ArrayList;
if (arrayList != null && arrayList.Count > 0)
{
return arrayList[0] is IDictionary<string, object>
? new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)))
: new List<object>(arrayList.Cast<object>());
}
return result;
}
}
#endregion
}
Or in the calling method:
public static dynamic DecodeJson(this string str)
{
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
dynamic result = null;
try
{
result = serializer.Deserialize(str, typeof(object));
} catch (ArgumentException ae)
{
Log.Output(ae.InnerException.Message);
}
return result;
}

You should modify the JavaScriptSerializer.MaxJsonLength Property
The maximum length of JSON strings. The default is 2097152 characters,
which is equivalent to 4 MB of Unicode string data.
public static dynamic DecodeJson(this string str)
{
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int.MaxValue; // The value of this constant is 2,147,483,647
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
dynamic result = null;
try
{
result = serializer.Deserialize(str, typeof(object));
} catch (ArgumentException ae)
{
Log.Output(ae.InnerException.Message);
}
return result;
}

Related

System.Text.Json to Newtonsoft Json

Is there a JsonElement and JsonValueKind equivalent in newtonsoft Json? What would be the correct code to port the below code that uses System.Text.Json to newtonsoft Json? Reason for my port in due to my dll not able to find the right System.Buffers assembly version. I followed all the advices I could get but still I am unable to resolve it. So thought of using Newtonsoft Json.
public static Dictionary<string, dynamic> JsonDeserialize(string Json)
{
JsonElement elm = JsonSerializer.Deserialize<JsonElement>(Json);
Dictionary<string, dynamic> dict = ElementToDict(elm);
return dict;
}
public static dynamic ElementToDict(JsonElement obj)
{
if (obj.ValueKind == JsonValueKind.Number)
{
return StringToDecimal(obj.GetRawText());
}
else if (obj.ValueKind == JsonValueKind.String)
{
return obj.GetString();
}
else if (obj.ValueKind == JsonValueKind.True || obj.ValueKind == JsonValueKind.False)
{
return obj.GetBoolean();
}
else if (obj.ValueKind == JsonValueKind.Object)
{
var map = obj.EnumerateObject().ToList();
var newMap = new Dictionary<String, dynamic>();
for (int i = 0; i < map.Count; i++)
{
newMap.Add(map[i].Name, ElementToDict(map[i].Value));
}
return newMap;
}
else if (obj.ValueKind == JsonValueKind.Array)
{
var items = obj.EnumerateArray().ToList();
var newItems = new ArrayList();
for (int i = 0; i < items.Count; i++)
{
newItems.Add(ElementToDict(obj[i]));
}
return newItems;
}
else
{
return null;
}
}
You can try using JToken and JTokenType:
var tok = JsonConvert.DeserializeObject<JToken>("{\"test\": 1}"); // or JToken.Parse
Console.WriteLine(tok.Type); // prints "Object"
Thanks to #guru-stron I sucessfully ported to Newtonsoft.Json. Following is my code:
public static Dictionary<string, dynamic> JsonDeserialize(string Json)
{
Console.WriteLine(Json);
var elm = JsonConvert.DeserializeObject<JToken>(Json);
// Replace double with decimal in the map
Dictionary<string, dynamic> dict = ElementToDict(elm);
return dict;
}
public static dynamic ElementToDict(JToken obj)
{
if (obj.Type == JTokenType.Float || obj.Type == JTokenType.Integer)
{
return StringToDecimal(obj.ToString());
}
else if (obj.Type == JTokenType.String)
{
return obj.ToString();
}
else if (obj.Type == JTokenType.Boolean)
{
return obj.ToObject<Boolean>();
}
else if (obj.Type == JTokenType.Object)
{
var map = obj.Children().ToList();
var newMap = new Dictionary<String, dynamic>();
foreach (JProperty m in map)
{
newMap.Add(m.Name, ElementToDict(m.Value));
}
return newMap;
}
else if (obj.Type == JTokenType.Array)
{
var items = obj.AsJEnumerable().ToList();
var newItems = new ArrayList();
for (int i = 0; i < items.Count; i++)
{
newItems.Add(ElementToDict(obj[i]));
}
return newItems;
}
else
{
return null;
}
}

Deserialize JSON.NET not knowing the type [duplicate]

This question already has answers here:
Deserializing polymorphic json classes without type information using json.net
(6 answers)
Closed 5 years ago.
So i'm coding a tcp/ip chat, and i wanted to use json.net to serialize the messages so i can send it through a network.
I got these two types of message:
class AlertMessage
{
public string Client { get; set; }
public ServerManager.Status Status { get; set; } //enum {Connect, Disconnect}
}
class TextMessage
{
public string Client { get; set; }
public string Message { get; set; }
}
to deserialize i need to use the method JsonConvert.DeserializeObject<T>(object), but on the client side i cant realy tell what type arrives, any help is welcome! thanks
If you have no idea about the json you are going to receive, you could use the
using System.Web.Script.Serialization;
namespace. (Easily search and add this reference)
I used the following code to convert any object into a valid json object.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
namespace MinimalMonitoringClient.Helper
{
public sealed class DynamicJsonConverter : JavaScriptConverter
{
public static object ConvertToObject(string data)
{
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
return serializer.Deserialize(data, typeof(object));
}
private static string CleanJson(string data)
{
//Remove leading and ending whitespaces.
data = data.Trim();
//Java Script notation fixes
if(data.StartsWith("["))
data = data.Remove(0, 1);
if(data.EndsWith("]"))
data = data.Remove(data.Length - 1, 1);
return data.Contains("={") ? data.Replace("={", ":{") : data;
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IEnumerable<Type> SupportedTypes
{
get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
}
#region Nested type: DynamicJsonObject
private sealed class DynamicJsonObject : DynamicObject
{
private readonly IDictionary<string, object> _dictionary;
public DynamicJsonObject(IDictionary<string, object> dictionary)
{
_dictionary = dictionary ?? throw new ArgumentNullException("dictionary");
}
public override string ToString()
{
var sb = new StringBuilder("{");
ToString(sb);
return sb.ToString();
}
private void ToString(StringBuilder sb)
{
var firstInDictionary = true;
foreach (var pair in _dictionary)
{
if (!firstInDictionary)
sb.Append(",");
firstInDictionary = false;
var value = pair.Value;
var name = pair.Key;
if (value is string)
{
sb.AppendFormat("{0}:\"{1}\"", name, value);
}
else if (value is IDictionary<string, object>)
{
new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);
}
else if (value is ArrayList)
{
sb.Append(name + ":[");
var firstInArray = true;
foreach (var arrayValue in (ArrayList)value)
{
if (!firstInArray)
sb.Append(",");
firstInArray = false;
if (arrayValue is IDictionary<string, object>)
new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);
else if (arrayValue is string)
sb.AppendFormat("\"{0}\"", arrayValue);
else
sb.AppendFormat("{0}", arrayValue);
}
sb.Append("]");
}
else
{
sb.AppendFormat("{0}:{1}", name, value);
}
}
sb.Append("}");
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (!_dictionary.TryGetValue(binder.Name, out result))
{
// return null to avoid exception. caller can check for null this way...
result = null;
return true;
}
result = WrapResultObject(result);
return true;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
if (indexes.Length == 1 && indexes[0] != null)
{
if (!_dictionary.TryGetValue(indexes[0].ToString(), out result))
{
// return null to avoid exception. caller can check for null this way...
result = null;
return true;
}
result = WrapResultObject(result);
return true;
}
return base.TryGetIndex(binder, indexes, out result);
}
private static object WrapResultObject(object result)
{
var dictionary = result as IDictionary<string, object>;
if (dictionary != null)
return new DynamicJsonObject(dictionary);
var arrayList = result as ArrayList;
if (arrayList != null && arrayList.Count > 0)
{
return arrayList[0] is IDictionary<string, object>
? new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)))
: new List<object>(arrayList.Cast<object>());
}
return result;
}
}
#endregion
}
}
Now simply convert any json into an object with
object jsonObject = DynamicJsonConverter.ConvertToObject(Json);
Hope that helps
Not sure if I have understood your question correctly, but you can Deserialize object it as dynamic. For example:
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);

Issue while deserializing Json's Multidimensional array field using System.Web.Script.Serialization.JavaScriptSerializer custom converter

Hi I had an issue while deserializing a json file which can have different data type( float and float array). I got a suggestion to use custom converter from Deserialize a json field with different data types without using Newtonsoft json but with System.Web.Script.Serialization.JavaScriptSerializer and it works fine for one dimensional array. I am currently being given jsons with two dimensional array and this converter couldn't handle the same. Below is a snippet from my json
{
"name" : "foo",
"value" : 457,
"comment" : "bla bla bla",
"data" : [
{
"name" : "bar",
"max" : 200,
"default" : [
[7,4],[2,2],[7,4],[1,1],[2,3],[3,1],[7,9]
]
}
]
}
Where the default can be sometimes primitive like
"default" : 3.56
Edit : Code used for custom serialization
class RootObjectConverter : CustomPropertiesConverter<RootObject>
{
const string ValuesName = "values";
protected override IEnumerable<string> CustomProperties
{
get { return new[] { ValuesName }; }
}
protected override void DeserializeCustomProperties(Dictionary<string, object> customDictionary, RootObject obj, JavaScriptSerializer serializer)
{
object itemCost;
if (customDictionary.TryGetValue(ValuesName, out itemCost) && itemCost != null)
obj.Values = serializer.FromSingleOrArray<float>(itemCost).ToArray();
}
protected override void SerializeCustomProperties(RootObject obj, Dictionary<string, object> dict, JavaScriptSerializer serializer)
{
obj.Values.ToSingleOrArray(dict, ValuesName);
}
}
public abstract class CustomPropertiesConverter<T> : JavaScriptConverter
{
protected abstract IEnumerable<string> CustomProperties { get; }
protected abstract void DeserializeCustomProperties(Dictionary<string, object> customDictionary, T obj, JavaScriptSerializer serializer);
protected abstract void SerializeCustomProperties(T obj, Dictionary<string, object> dict, JavaScriptSerializer serializer);
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
// Detach custom properties
var customDictionary = new Dictionary<string, object>();
foreach (var key in CustomProperties)
{
object value;
if (dictionary.TryRemoveInvariant(key, out value))
customDictionary.Add(key, value);
}
// Deserialize and populate all members other than "values"
var obj = new JavaScriptSerializer().ConvertToType<T>(dictionary);
// Populate custom properties
DeserializeCustomProperties(customDictionary, obj, serializer);
return obj;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
// Generate a default serialization. Is there an easier way to do this?
var defaultSerializer = new JavaScriptSerializer();
var dict = defaultSerializer.Deserialize<Dictionary<string, object>>(defaultSerializer.Serialize(obj));
// Remove default serializations of custom properties, if present
foreach (var key in CustomProperties)
{
dict.RemoveInvariant(key);
}
// Add custom properties
SerializeCustomProperties((T)obj, dict, serializer);
return dict;
}
public override IEnumerable<Type> SupportedTypes
{
get { return new[] { typeof(T) }; }
}
}
public static class JavaScriptSerializerObjectExtensions
{
public static void ReplaceInvariant<T>(this IDictionary<string, T> dictionary, string key, T value)
{
RemoveInvariant(dictionary, key);
dictionary.Add(key, value);
}
public static bool TryRemoveInvariant<T>(this IDictionary<string, T> dictionary, string key, out T value)
{
if (dictionary == null)
throw new ArgumentNullException();
var keys = dictionary.Keys.Where(k => string.Equals(k, key, StringComparison.OrdinalIgnoreCase)).ToArray();
if (keys.Length == 0)
{
value = default(T);
return false;
}
else if (keys.Length == 1)
{
value = dictionary[keys[0]];
dictionary.Remove(keys[0]);
return true;
}
else
{
throw new ArgumentException(string.Format("Duplicate keys found: {0}", String.Join(",", keys)));
}
}
public static void RemoveInvariant<T>(this IDictionary<string, T> dictionary, string key)
{
if (dictionary == null)
throw new ArgumentNullException();
foreach (var actualKey in dictionary.Keys.Where(k => string.Equals(k, key, StringComparison.OrdinalIgnoreCase)).ToArray())
dictionary.Remove(actualKey);
}
public static void ToSingleOrArray<T>(this ICollection<T> list, IDictionary<string, object> dictionary, string key)
{
if (dictionary == null)
throw new ArgumentNullException();
if (list == null || list.Count == 0)
dictionary.RemoveInvariant(key);
else if (list.Count == 1)
dictionary.ReplaceInvariant(key, list.First());
else
dictionary.ReplaceInvariant(key, list.ToArray());
}
public static List<T> FromSingleOrArray<T>(this JavaScriptSerializer serializer, object value)
{
if (value == null)
return null;
if (value.IsJsonArray())
{
return value.AsJsonArray().Select(i => serializer.ConvertToType<T>(i)).ToList();
}
else
{
return new List<T> { serializer.ConvertToType<T>(value) };
}
}
public static bool IsJsonArray(this object obj)
{
if (obj is string || obj is IDictionary)
return false;
return obj is IEnumerable;
}
public static IEnumerable<object> AsJsonArray(this object obj)
{
return (obj as IEnumerable).Cast<object>();
}
}
and used it like
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new RootObjectConverter() });
var root = serializer.Deserialize<RootObject>(json);
Can anyone help me on how to proceed on this?
I fixed the issue by using a Dictionary instead of a class object for parsing different data type fields and accessing the respective property with the key value. Thanks Everyone for your suggestions.

With C# how to pass list of dynamically generated collection as JSON array to WCF web service

My objective:
To create a list of objects with dynamically generated properties which can be passed to WCF service parameter as a JSON array.
Final outcome is the following:
[{id:2, name:"John", country:"Germany"},...] or
[{id:3, city:"Sydney"},...]
From WCF I don't have the convenience of working with a class, i.e. "Thing" so I could do this:
public List<Thing> MyThings {get; set;}
I do not know what the properties are at run time. I have experimented with System.Dynamic namespace using
List<ExpandoObject>
Unfortunately, dynamic code will give me the error message:
Dynamic operations can only be performed in homogenous AppDomain.
To allow dynamic code to run I would have to change legacyCasModel tag in the web.config. Unfortunately I cannot change the web.config.
Another option is to use Dictionary array but I do not want the key/value pair format:
[{key:id}, {value:2}]
for I'm interested in
[{id:2, name:"John"}]
My challenge is that I don't know what the properties are at run time otherwise it would be easy to convert a List<T> to array.
Any suggestion?
To pass list of dynamically generated collection as JSON array to WCF web service, you can use ExpandoObject and any serializer, I have used Newtonsoft.Json
dynamic obj1 = new ExpandoObject();
obj1.id = 1;
obj1.name = "TPS";
dynamic obj2 = new ExpandoObject();
obj2.id = 2;
obj2.name = "TPS_TPS";
var Objects = new List<ExpandoObject>();
Objects.Add(flexible);
Objects.Add(flexible2);
var serialized = JsonConvert.SerializeObject(l); // JsonConvert - from Newtonsoft.Json
To Deserialize and retrieve the object dynamically i have used JavaScriptSerializer, you can also use System.Web.Helpers.Json.Decode()
All you need is this code and reference to a System.Web.Extensions from your project :
public sealed class DynamicJsonConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override IEnumerable<Type> SupportedTypes
{
get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
}
#region Nested type: DynamicJsonObject
private sealed class DynamicJsonObject : DynamicObject
{
public readonly IDictionary<string, object> _dictionary;
public DynamicJsonObject(IDictionary<string, object> dictionary)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
_dictionary = dictionary;
}
public override string ToString()
{
var sb = new StringBuilder("{");
ToString(sb);
return sb.ToString();
}
private void ToString(StringBuilder sb)
{
var firstInDictionary = true;
foreach (var pair in _dictionary)
{
if (!firstInDictionary)
sb.Append(",");
firstInDictionary = false;
var value = pair.Value;
var name = pair.Key;
if (value is string)
{
sb.AppendFormat("{0}:\"{1}\"", name, value);
}
else if (value is IDictionary<string, object>)
{
new DynamicJsonObject((IDictionary<string, object>)value).ToString(sb);
}
else if (value is ArrayList)
{
sb.Append(name + ":[");
var firstInArray = true;
foreach (var arrayValue in (ArrayList)value)
{
if (!firstInArray)
sb.Append(",");
firstInArray = false;
if (arrayValue is IDictionary<string, object>)
new DynamicJsonObject((IDictionary<string, object>)arrayValue).ToString(sb);
else if (arrayValue is string)
sb.AppendFormat("\"{0}\"", arrayValue);
else
sb.AppendFormat("{0}", arrayValue);
}
sb.Append("]");
}
else
{
sb.AppendFormat("{0}:{1}", name, value);
}
}
sb.Append("}");
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (!_dictionary.TryGetValue(binder.Name, out result))
{
// return null to avoid exception. caller can check for null this way...
result = null;
return true;
}
result = WrapResultObject(result);
return true;
}
public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
{
if (indexes.Length == 1 && indexes[0] != null)
{
if (!_dictionary.TryGetValue(indexes[0].ToString(), out result))
{
// return null to avoid exception. caller can check for null this way...
result = null;
return true;
}
result = WrapResultObject(result);
return true;
}
return base.TryGetIndex(binder, indexes, out result);
}
private static object WrapResultObject(object result)
{
var dictionary = result as IDictionary<string, object>;
if (dictionary != null)
return new DynamicJsonObject(dictionary);
var arrayList = result as ArrayList;
if (arrayList != null && arrayList.Count > 0)
{
return arrayList[0] is IDictionary<string, object>
? new List<object>(arrayList.Cast<IDictionary<string, object>>().Select(x => new DynamicJsonObject(x)))
: new List<object>(arrayList.Cast<object>());
}
return result;
}
}
#endregion
}
How to Use :
string jsonString = ".......";
var serializer = new JavaScriptSerializer();
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
dynamic data = serializer.Deserialize(jsonString, typeof(object));
foreach (var d in data)
{
var id = d.id;
var name = d.name;
}

Exception when deserializing json

I have 2 methods
public static string SerializeObject<T>(T value)
{
if (value == null)
{
return null;
}
var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };
var jsonString = JsonConvert.SerializeObject(dictionaryObject);
return jsonString;
}
and
public static T DeserializeObject<T>(string jsonString)
{
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString);
return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString());
}
When I deserialize a json string with type
ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>
I have an exception:
Could not convert string '[1, 1]' to dictionary key type 'System.Collections.Generic.KeyValuePair`2[System.Int64,System.Int64]'. Create a TypeConverter to convert from the string to the key type object. Path '[1, 1]', line 2, position 12.
So Can someone show me the right code for it to work?
Here is my code:
var test = new ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>();
test.TryAdd(new KeyValuePair<long, long>(1, 1), new List<string> { "Test" });
var se = SerializeObject(test);
var de = DeserializeObject<ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>>(se);
I'm not sure if this is the best solution, but, please, try this:
1) create ContractResolver as described in this topic.
class DictionaryAsArrayResolver : DefaultContractResolver
{
protected override JsonContract CreateContract(Type objectType)
{
if (objectType.GetInterfaces().Any(i => i == typeof(IDictionary) ||
(i.IsGenericType &&
i.GetGenericTypeDefinition() == typeof(IDictionary<,>))))
{
return base.CreateArrayContract(objectType);
}
return base.CreateContract(objectType);
}
}
2) Change a bit your Serialize/Deserialize methods:
public static string SerializeObject<T>(T value, JsonSerializerSettings settings)
{
if (value == null)
{
return null;
}
var dictionaryObject = new Dictionary<string, object> { { typeof(T).Name, value } };
var jsonString = JsonConvert.SerializeObject(dictionaryObject, settings);
return jsonString;
}
public static T DeserializeObject<T>(string jsonString, JsonSerializerSettings settings)
{
var objectValue = JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonString, settings);
return JsonConvert.DeserializeObject<T>(objectValue.Values.First().ToString(), settings);
}
3) Check the test:
[TestMethod]
public void Test()
{
var test = new ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>();
test.TryAdd(new KeyValuePair<long, long>(1, 1), new List<string> { "Test" });
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ContractResolver = new DictionaryAsArrayResolver();
var se = SerializeObject(test, settings);
var de = DeserializeObject<ConcurrentDictionary<KeyValuePair<long, long>, IList<string>>>(se, settings);
}
I hope, it helps =)

Categories