Click here to Skip to main content
15,904,416 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: JSON is better than XML Pin
honey the codewitch9-Jan-20 13:22
mvahoney the codewitch9-Jan-20 13:22 
GeneralRe: JSON is better than XML Pin
PIEBALDconsult9-Jan-20 13:40
mvePIEBALDconsult9-Jan-20 13:40 
GeneralRe: JSON is better than XML Pin
honey the codewitch9-Jan-20 13:58
mvahoney the codewitch9-Jan-20 13:58 
GeneralRe: JSON is better than XML Pin
PIEBALDconsult9-Jan-20 14:31
mvePIEBALDconsult9-Jan-20 14:31 
JokeRe: JSON is better than XML Pin
Dar Brett9-Jan-20 16:27
Dar Brett9-Jan-20 16:27 
GeneralRe: JSON is better than XML Pin
honey the codewitch9-Jan-20 16:53
mvahoney the codewitch9-Jan-20 16:53 
GeneralRe: JSON is better than XML Pin
Marc Clifton10-Jan-20 2:26
mvaMarc Clifton10-Jan-20 2:26 
GeneralRe: JSON is better than XML Pin
Richard Deeming10-Jan-20 3:21
mveRichard Deeming10-Jan-20 3:21 
Not sure why you'd be using a .NET 1-era collection instead of a Dictionary<string, string>; but it's fairly simple to fix the issue with a custom JSON converter:
C#
public class StringDictionaryConverter : JsonConverter
{
    public override bool CanRead => true;
    public override bool CanWrite => true;
    public override bool CanConvert(Type objectType) => typeof(StringDictionary).IsAssignableFrom(objectType);
    
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        // Serialize the StringDictionary "properly":
        var dictionary = (StringDictionary)value;
        writer.WriteStartObject();
        foreach (DictionaryEntry entry in dictionary)
        {
            writer.WritePropertyName((string)entry.Key);
            writer.WriteValue(entry.Value);
        }
        writer.WriteEndObject();
    }
    
    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.TokenType == JsonToken.Null) return null;
        
        var dictionary = existingValue as StringDictionary ?? (StringDictionary)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
        switch (reader.TokenType)
        {
            case JsonToken.StartObject:
            {
                // Serialized with converter:
                var values = serializer.Deserialize<Dictionary<string, string>>(reader);
                foreach (var pair in values)
                {
                    dictionary.Add(pair.Key, pair.Value);
                }
                break;
            }
            case JsonToken.StartArray:
            {
                // Serialized without converter:
                var values = serializer.Deserialize<List<KeyValuePair<string, string>>>(reader);
                foreach (var pair in values)
                {
                    dictionary.Add(pair.Key, pair.Value);
                }
                break;
            }
            default:
            {
                throw new JsonSerializationException($"Unknown token {reader.TokenType} at path {reader.Path}");
            }
        }
        
        return dictionary;
    }
}
Example:
C#
var source = new StringDictionary
{
    ["xml"] = "sucks",
    ["json"] = "rules",
};

string json = JsonConvert.SerializeObject(source, new StringDictionaryConverter());
var result = JsonConvert.DeserializeObject<StringDictionary>(json, new StringDictionaryConverter());
(Based on [Fork] [Fork] questions/52721999/modelstate-error-newtonsoft-json-jsonserializationexception-cannot-populate-l?noredirect=1#comment92399021_52721999 | C# Online Compiler | .NET Fiddle[^])



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralThought of the Day Pin
OriginalGriff9-Jan-20 4:34
mveOriginalGriff9-Jan-20 4:34 
GeneralRe: Thought of the Day Pin
Daniel Pfeffer9-Jan-20 4:59
professionalDaniel Pfeffer9-Jan-20 4:59 
GeneralRe: Thought of the Day Pin
Jane's Diary9-Jan-20 8:08
Jane's Diary9-Jan-20 8:08 
GeneralRe: Thought of the Day Pin
Daniel Pfeffer9-Jan-20 9:20
professionalDaniel Pfeffer9-Jan-20 9:20 
GeneralRe: Thought of the Day Pin
PIEBALDconsult9-Jan-20 5:16
mvePIEBALDconsult9-Jan-20 5:16 
QuestionRe: Thought of the Day Pin
lopatir9-Jan-20 5:29
lopatir9-Jan-20 5:29 
GeneralRe: Thought of the Day Pin
Christian Graus9-Jan-20 10:32
protectorChristian Graus9-Jan-20 10:32 
GeneralMy main problem now is finding direction Pin
honey the codewitch9-Jan-20 3:54
mvahoney the codewitch9-Jan-20 3:54 
GeneralRe: My main problem now is finding direction Pin
W Balboos, GHB9-Jan-20 4:01
W Balboos, GHB9-Jan-20 4:01 
GeneralRe: My main problem now is finding direction Pin
honey the codewitch9-Jan-20 4:13
mvahoney the codewitch9-Jan-20 4:13 
GeneralRe: My main problem now is finding direction Pin
DRHuff9-Jan-20 8:25
DRHuff9-Jan-20 8:25 
GeneralRe: My main problem now is finding direction Pin
honey the codewitch9-Jan-20 8:43
mvahoney the codewitch9-Jan-20 8:43 
GeneralRe: My main problem now is finding direction Pin
Kris Lantz9-Jan-20 8:51
professionalKris Lantz9-Jan-20 8:51 
GeneralRe: My main problem now is finding direction Pin
PIEBALDconsult9-Jan-20 4:22
mvePIEBALDconsult9-Jan-20 4:22 
GeneralRe: My main problem now is finding direction Pin
Kris Lantz9-Jan-20 7:38
professionalKris Lantz9-Jan-20 7:38 
GeneralRe: My main problem now is finding direction Pin
John R. Shaw9-Jan-20 18:42
John R. Shaw9-Jan-20 18:42 
GeneralRe: My main problem now is finding direction Pin
TheGreatAndPowerfulOz9-Jan-20 9:56
TheGreatAndPowerfulOz9-Jan-20 9:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.