You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
List<KeyValuePair<TKey,TValue>> Not support well #116
When I use a list of struct KeyValuePair, Mapper.toJson works. But when I want to parse the Json to object only get default value.
It doesn’t bother,because I can just use a struct to contain the data.
But i will appreciate it,if you can solve the problem
when i use Dictionary<TKey,TValue>,only <string,string> as key and value can work. when i try to use int or float or double or custom struct , it throws out "can not convert xxx into string" ( i just translate the error info into English, which is in my native language)
instead of List<KeyValuePair<TKey,TValue>>
JsonMapper.ToObject works well again.(actually i know well that it will work,because i have used this kind of structure for a long time. )
I Have Same Issue.
I attach sample code and result. Please Check.
using System;
using System.Collections.Generic;
public static class Program
{
public static void Main()
{
// Serialize object to JSON
var toObject = new HelloWorld();
toObject.dictionary.Add(101, true);
var toJson = LitJson.JsonMapper.ToJson(toObject);
Console.WriteLine("To JSON: {0}", toJson);
// Serialize JSON to object
var fromJson = "{\"Message\":\"Hello world!\"}";
var fromObject = LitJson.JsonMapper.ToObject<HelloWorld>(fromJson);
Console.WriteLine("From json: {0}", fromObject.dictionary);
}
public class HelloWorld
{
public Dictionary<int, bool> dictionary = new Dictionary<int, bool>();
}
}
Run-time exception (line 11): Unable to cast object of type 'System.Int32' to type 'System.String'.
Stack Trace:
[System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.]
at LitJson.JsonMapper.WriteValue(Object obj, JsonWriter writer, Boolean writer_is_private, Int32 depth)
at LitJson.JsonMapper.WriteValue(Object obj, JsonWriter writer, Boolean writer_is_private, Int32 depth)
at LitJson.JsonMapper.ToJson(Object obj)
at Program.Main() :line 11
Activity
devlead commentedon Jan 22, 2020
Does it work if you use
Dictionary<TKey, TValue>?MuGdxy commentedon Jan 24, 2020
when i use
Dictionary<TKey,TValue>,only <string,string> as key and value can work. when i try to useintorfloatordoubleor custom struct , it throws out "can not convert xxx into string" ( i just translate the error info into English, which is in my native language)MuGdxy commentedon Jan 24, 2020
and come back to
List<KeyValuePair<TKey,TValue>>:when i use
List<MyStruct>withinstead of
List<KeyValuePair<TKey,TValue>>JsonMapper.ToObject works well again.(actually i know well that it will work,because i have used this kind of structure for a long time. )
DevBear89 commentedon Mar 9, 2021
I Have Same Issue.
I attach sample code and result. Please Check.