Wiki

Clone wiki

HaveBoxJSON / Basic usage

Home - News - Documentation

Given JSON:

#!JSONLexer

{
  "Date": "2013-11-27T22:43:37.5065305+01:00",
  "GlobalId": "c4ec0f95-002d-480a-994f-7677ee14f633",
  "Id": 123,
  "Name": "John \"Unknown\" Doe",
  "IsHappy": true,
  "Kids": [
    "Ringler",
    "Dingler"
  ],
  "Balance": -42.01,
  "Address": {
    "Street": "The Street",
    "City": {
      "Zip": 90210,
      "CityName": "Beverly Hills"
    }
  },
  "PhoneNumbers": {
    "Home": "1234",
    "work": "5678"
  }
}

And DTO:

#!CSharpLexer

        public class Person
        {
            public DateTime Date { get; set; }
            public Guid GlobalId { get; set; }
            public int Id { get; set; }
            public string Name { get; set; }
            public bool IsHappy { get; set; }
            public IList<string> Kids { get; set; }
            public decimal Balance { get; set; }
            public Address Address { get; set; }
            public IDictionary<string, string> PhoneNumbers { get; set; }
        }

        public class Address
        {
            public string Street { get; set; }
            public City City { get; set; }
        }

        public class City
        {
            public int Zip { get; set; }
            public string CityName { get; set; }
        }

Basic example of usage can be

#!CSharpLexer

using HaveBoxJSON;
using System;
using System.Collections.Generic;

namespace BasicSerializingDeserializing
{
    public class Program
    {
        public class Person
        {
            public DateTime Date { get; set; }
            public Guid GlobalId { get; set; }
            public int Id { get; set; }
            public string Name { get; set; }
            public bool IsHappy { get; set; }
            public IList<string> Kids { get; set; }
            public decimal Balance { get; set; }
            public Address Address { get; set; }
            public IDictionary<string, string> PhoneNumbers { get; set; }
        }

        public class Address
        {
            public string Street { get; set; }
            public City City { get; set; }
        }

        public class City
        {
            public int Zip { get; set; }
            public string CityName { get; set; }
        }

        static void Main(string[] args)
        {
            var json = "{\"Date\":\"2013-11-27T22:43:37.5065305+01:00\",\"GlobalId\":\"c4ec0f95-002d-480a-994f-7677ee14f633\",\"Id\":123,\"Name\":\"John \\\"Unknown\\\" Doe\",\"IsHappy\":true,\"Kids\":[\"Ringler\",\"Dingler\"],\"Balance\":-42.01,\"Address\":{\"Street\":\"The Street\",\"City\":{\"Zip\":90210,\"CityName\":\"Beverly Hills\"}},\"PhoneNumbers\":{\"Home\":\"1234\",\"work\":\"5678\"}}";
            var jsonConverter = new JsonConverter();

            var person = jsonConverter.Deserialize<Person>(json);

            Console.WriteLine("Person name: {0}  Id: {1} and Home phonenumber: {2}", person.Name, person.Id, person.PhoneNumbers["Home"]);

            var sameJson = jsonConverter.Serialize(person);
            Console.WriteLine("JSON has changes {0}", json != sameJson);

            Console.WriteLine("Changes person's city to 12345 NoWhere");
            person.Address.City.CityName = "NoWhere";
            person.Address.City.Zip = 12345;

            var changedJson = jsonConverter.Serialize(person);
            Console.WriteLine("JSON has changes {0}", json != changedJson);
            Console.WriteLine("Changed JSON {0}", changedJson);
        }
    }
}

Output:

#!CSharpLexer

Person name: John "Unknown" Doe  Id: 123 and Home phonenumber: 1234
JSON has changes False
Changes person's city to 12345 NoWhere
JSON has changes True
Changed JSON {"Date":"2013-11-27T22:43:37.5065305+01:00","GlobalId":"c4ec0f95-00
2d-480a-994f-7677ee14f633","Id":123,"Name":"John \"Unknown\" Doe","IsHappy":true
,"Kids":["\"Ringler\"","\"Dingler\""],"Balance":-42.01,"Address":{"Street":"The
Street","City":{"Zip":12345,"CityName":"NoWhere"}},"PhoneNumbers":{"Home":"1234"
,"work":"5678"}}
Press any key to continue . . .

Updated