GithubHelp home page GithubHelp logo

thetrigger / json.netmf Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nanoframework/json.netmf

0.0 2.0 0.0 196 KB

Json Serializer and Deserializer Library for nanoFramework and .NET Micro Framework

License: MIT License

C# 100.00%

json.netmf's Introduction

Quality Gate Status Reliability Rating License #yourfirstpr Discord

nanoFramework.Json (Json.NetMF)

Welcome to the JSON Serializer and Deserializer library for nanoFramework and .NET Micro Framework.

This library is now being maintained by the nanoFramework team after being transferred from Matt Weimer. The library was originally forked and modified from Mike Jones's JSON Serialization and Deserialization library.

Its hopefully faster, more lightweight, and more robust.

Build status

Component Build Status NuGet Package
nanoFramework.Json Build Status NuGet
nanoFramework.Json (preview) Build Status
Json.NetMF Build Status NuGet
Json.NetMF (preview) Build Status

Requirements

  • nanoFramework 1.0 or higher
  • Microsoft .NET Micro Framework 4.2 or higher

Example Usage

An instance of the JsonSerializer class can be instantiated using the constructor. This will allow the serializer to remember your DateTime format preference.

JsonSerializer serializer = new JsonSerializer(DateTimeFormat.Default);
string json = serializer.Serialize(o);

Additionally, you can just call the static SerializeObject/DeserializeString methods and specify your DateTime format preference as an argument.

string json = JsonSerializer.SerializeObject(o,DateTimeFormat.Default);

DateTime preference is optional, you can simply use:

string json = serializer.Serialize(o);
//or
string json = JsonSerializer.SerializeObject(o);

Serialization

Pretty much any object can be serialized.

Any object that implements IEnumerable will get serialized into a JSON array. This include arrays, ArrayList, ICollection, IList, Queue and Stack.

ArrayList arrayList = new ArrayList() { 1, true, "hello world", null };

string json = JsonSerializer.SerializeObject(arrayList);

Debug.Print(json);
// Output: [1,true,"hello world",null]

Any object that implements IDictionary will get serialized into a JSON object. If an object implements both IDictionary and IEnumerable it gets serialized as a JSON object.

Hashtable hashtable = new Hashtable();
hashtable.Add("a bool", true);
hashtable.Add("a null", null);
hashtable.Add("a string", "hello world");
hashtable.Add("a number", 1);
hashtable.Add("an array", new object[] {1, 2, 3 });

string json = JsonSerializer.SerializeObject(hashtable);

Debug.Print(json);
// Output: {"a string":"hello world","a null":null,"a bool":true,"a number":1,"an array":[1,2,3]}

Other objects will be serialized using their public properties. Only properties that have a public getter will be serialized.

DateTime objects can be serialized, and their format in JSON will be ISO 8601 format by default.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }
}

...

Person person = new Person() { FirstName = "John", LastName = "Doe", Birthday = new DateTime(1988, 4, 23) };

string json = JsonSerializer.SerializeObject(person);

Debug.Print(json);
// Output: {"Birthday":"1988-04-23T00:00:00.000Z","LastName":"Doe","FirstName":"John"}

Deserialization

Deserialization will parse your JSON string and return it contents in either ArrayList, Hashtable, double, long, string, null, bool. You will need to know what type you are expecting and cast the return object to that type or manually check the type and then cast.

// JSON array to ArrayList
string json = "[\"hello\", \"world\", \"!!!\"]";
ArrayList arrayList = JsonSerializer.DeserializeString(json) as ArrayList;
Debug.Print(arrayList[0] + " " + arrayList[1] + arrayList[2]);
// Output: "Hello World!!!"

// JSON object to Hashtable
string json = "{\"firstName\":\"John\",\"lastName\":\"Doe\"}";
Hashtable hashTable = JsonSerializer.DeserializeString(json) as Hashtable;

Debug.Print(hashTable["firstName"] + " " + hashTable["lastName"]);
// Output: "John Doe"

// Checking return types
string json = "[1, 2.345, -5, -232323.7878678]";
object deserializedObject = JsonSerializer.DeserializeString(json);

if (deserializedObject is ArrayList)
{
    ArrayList arrayList = deserializedObject as ArrayList;
    foreach (object o in arrayList)
    {
        if (o is long)
        {
            long l = (long) o;
            Debug.Print("long: " + l);
        }
        else if (o is double)
        {
            double d = (double)o;
            Debug.Print("double: " + d);
        }
    }
}
// Output (maybe some prescion errors in the to double.ToString:
// long: 1
// double: 2.3450000000000002
// long: -5
// double: -232323.78786779998

Extensions methods are provided to parse a DateTime string to a DateTime object

string json = "{\"firstName\":\"John\",\"lastName\":\"Doe\",\"birthDay\":\"1985-04-27T00:00:00.000Z\"}";

Hashtable hashTable = JsonSerializer.DeserializeString(json) as Hashtable;

DateTime birthday = DateTimeExtensions.FromIso8601(hashTable["birthDay"] as string);

Debug.Print(birthday.ToString());
// Output: "04/27/1985 00:00:00"

NuGet packages

The nanoFramework version is available through NuGet

PM> Install-Package nanoFramework.Json

The .NetMF version is available through NuGet

PM> Install-Package Json.NetMF

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

License

The nanoFramework.Json is licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behavior in our community.

json.netmf's People

Contributors

adriansoundy avatar andylyonette avatar haeberle avatar josesimoes avatar mweimer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.