Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JIT. Best way serialize to json

I need to create a custom json for the jit library. Should I use additional C# logic or somehow to extend JsonSerializer. Json should be like this -->

var json = {
    "children": [
 {
     "children": [
     {
         "children": [],
         "data": {
             "playcount": "276",
             "$color": "#8E7032",
             "image": "http://userserve-ak.last.fm/serve/300x300/11403219.jpg",
             "$area": 276
         },
         "id": "album-Thirteenth Step",
         "name": "Thirteenth Step"
     }
}] 

}

like image 324
Alexandr Avatar asked Jun 15 '12 11:06

Alexandr


People also ask

What is the difference between JSON and serialization?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.

How do you serialize JSON data in Python?

Working With JSON Data in Python The json module exposes two methods for serializing Python objects into JSON format. dump() will write Python data to a file-like object. We use this when we want to serialize our Python data to an external JSON file. dumps() will write Python data to a string in JSON format.

How do you serialize and deserialize an object in C# using JSON?

To serialize a . Net object to JSON string use the Serialize method. It's possible to deserialize JSON string to . Net object using Deserialize<T> or DeserializeObject methods.

How do I deserialize JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.


2 Answers

Using Json.Net

public void Test()
{
    Node root = new Node();
    Node child = new Node();
    Data data = new Data() { Area = 276, Color = "#8E7032", PlayCount = "276", Image = "http://userserve-ak.last.fm/serve/300x300/11403219.jpg" };
    Node grandChild = new Node() { Id = "album-Thirteenth Step", Name = "Thirteenth Step", Data = data };

    root.Children.Add(child);
    child.Children.Add(grandChild);

    var json = JsonConvert.SerializeObject(
                              root, 
                              new JsonSerializerSettings() {  
                                  NullValueHandling= NullValueHandling.Ignore,
                                  Formatting= Newtonsoft.Json.Formatting.Indented
                              });
}

public class Node
{
    [JsonProperty("children")]
    public List<Node> Children = new List<Node>();

    [JsonProperty("data")]
    public Data Data;

    [JsonProperty("id")]
    public string Id;

    [JsonProperty("name")]
    public string Name;
}

public class Data
{
    [JsonProperty("playcount")]
    public string PlayCount;

    [JsonProperty("$color")]
    public string Color;

    [JsonProperty("image")]
    public string Image;

    [JsonProperty("$area")]
    public int Area;
}
like image 156
L.B Avatar answered Nov 15 '22 13:11

L.B


Have you though about Json.net?

http://json.codeplex.com/

At least you will have a good level of customization room + a better serializer

like image 41
Ademar Avatar answered Nov 15 '22 15:11

Ademar