Open MuGdxy opened 4 years ago
Does it work if you use Dictionary<TKey, TValue>
?
[Serializable]
public class ToSave
{
public Dictionary<string, string> dic = new Dictionary<string, string>();
}
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)
and come back to List<KeyValuePair<TKey,TValue>>
:
when i use List<MyStruct>
with
public struct MyStruct
{
public string Key;
public int Value;
}
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
List<KeyValuePair<TKey,TValue>> Not support well
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
Sent from PPHub