Skip to content

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

Open

Description

@MuGdxy

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

Activity

  1. devlead commented on Jan 22, 2020

    @devlead
    Member

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

  2. MuGdxy commented on Jan 24, 2020

    @MuGdxy
    Author
        [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)

  3. MuGdxy commented on Jan 24, 2020

    @MuGdxy
    Author

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

  4. DevBear89 commented on Mar 9, 2021

    @DevBear89

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions