Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List<KeyValuePair<TKey,TValue>> Not support well #116

Open
MuGdxy opened this issue Jan 22, 2020 · 4 comments
Open

List<KeyValuePair<TKey,TValue>> Not support well #116

MuGdxy opened this issue Jan 22, 2020 · 4 comments

Comments

@MuGdxy
Copy link

MuGdxy commented Jan 22, 2020

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

@devlead
Copy link
Member

devlead commented Jan 22, 2020

Does it work if you use Dictionary<TKey, TValue>?

@MuGdxy
Copy link
Author

MuGdxy commented Jan 24, 2020

    [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)

@MuGdxy
Copy link
Author

MuGdxy commented Jan 24, 2020

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. )

@DevBear89
Copy link

DevBear89 commented Mar 9, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants