How do I de/serialize JSON in WinRT?
Asked Answered
S

3

18

How do I take an object and convert it to a JSON string and then back into that object from a string, specifically, in WinRT for my Windows 8 Metro application?

Steam answered 10/6, 2012 at 1:47 Comment(0)
S
38

Like this:

using System.IO;
using System.Runtime.Serialization.Json;
using System.Text;

public static T Deserialize<T>(string json)
{
    var _Bytes = Encoding.Unicode.GetBytes(json);
    using (MemoryStream _Stream = new MemoryStream(_Bytes))
    {
        var _Serializer = new DataContractJsonSerializer(typeof(T));
        return (T)_Serializer.ReadObject(_Stream);
    }
}

public static string Serialize(object instance)
{
    using (MemoryStream _Stream = new MemoryStream())
    {
        var _Serializer = new DataContractJsonSerializer(instance.GetType());
        _Serializer.WriteObject(_Stream, instance);
        _Stream.Position = 0;
        using (StreamReader _Reader = new StreamReader(_Stream)) 
        { return _Reader.ReadToEnd(); }
    }
}
Steam answered 10/6, 2012 at 1:47 Comment(1)
DataContractJsonSerializer requires that classes and members are marked with DataContract and DataMember. Json.net does not. The former is safer because not all classes can be fully rebuilt from their properties alone. The latter is faster because you don't always control the objects you are serializing and creating a serializable intermediary class takes a bit more time.Compliance
S
4

First generate C# classes with http://json2csharp.com/ Then use http://james.newtonking.com/pages/json-net.aspx for parsing

My user class currently looks like this:

public class User
{
    public string id { get; set; }
    public string username { get; set; }
    public string full_name { get; set; }
    public string profile_picture { get; set; }
    public string bio { get; set; }
    public string website { get; set; }
    public Counts counts { get; set; }

    public static User SingleFromJSON(string jsonString)
    {
        return JsonConvert.DeserializeObject<SingleUser>(jsonString).data;
    }

    public static User MultipleFromJSON(string jsonString)
    {
        return JsonConvert.DeserializeObject<SingleUser>(jsonString).data;
    }

    private class SingleUser
    {
        public User data { get; set; }
    }

    private class MultipleUsers
    {
        public List<User> data { get; set; }
    }
}

public class Counts
{
    public int media { get; set; }
    public int follows { get; set; }
    public int followed_by { get; set; }
}

Super easy :D

Stuck answered 8/9, 2012 at 17:10 Comment(1)
That's another way, for sure. Not everyone likes outside libraries like that. But json.net is well respected. (and fast)Steam
G
0

OP asked for an answer that pertained to metro apps on Windows 8, but this question comes up when doing a basic search for serializing/de-serializing jsons for WinRT, so I'm adding this answer for those who are looking for a more modern approach.

Modern WinRT has built-in handlers for this challenge, so no need to use JSON.net

Use JsonObject from Windows.Data.Json

For serializing use Parse or TryParse.

For de-serializing but still in JSON format, use Stringify.

Guienne answered 28/1, 2019 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.