Code Monkey home page Code Monkey logo

mojojson's Introduction

MojoJson

MojoJson is an extremely simple and super fast JSON parser. The parser supports all standard Json formats and provides simple APIs for visit different types of the Json values. Also the core algorithm can be easily implemented by various programming languages.

The current implementations as follow:

  • For C#.

    The core parsing code only 400 lines, and the implementation only use the core .NET lib, and just has one file that can be easily integrated into any C# project.

  • For C.

    The core parsing code only 300 lines, and the implementation only use the C standard lib, and just has one C file that can be easily integrated into any C project.

    Notice: the only different from C# is that the C code does not support SetEscapeString API to handle escape strings.

License

MojoJson is licensed under the MIT License.

How to use

  • For C#

    • Parse Json string.
    var jsonValue = MojoJson.Json.Parse(string jsonString);
    • Whether to convert escaped strings when ParseString.
    /// default false
    MojoJson.Json.SetEscapeString(bool isEscapeString)
    • JsonValue is primitive type.
    public string AsString();
    public float  AsFloat();
    public int    AsInt();
    public bool   AsBool();
    public bool   IsNull();
    • JsonValue is JsonObject
    /// Get the JsonObject, that is a set of k-v pairs, and each value is JsonValue.
    public Dictionary<string, JsonValue> AsObject();
    
    /// Get the JsonValue from JsonObject by key.
    public JsonValue AsObjectGet(string key);
    
    /// Get the JsonObject from JsonObject by key.
    public Dictionary<string, JsonValue> AsObjectGetObject(string key);
    
    /// Get the JsonArray from JsonObject by key.
    public List<JsonValue> AsObjectGetArray(string key);
    
    /// Get the string from JsonObject by key.
    public string AsObjectGetString(string key);
    
    /// Get the float from JsonObject by key.
    public float AsObjectGetFloat(string key);
    
    /// Get the int from JsonObject by key.
    public int AsObjectGetInt(string key);
    
    /// Get the bool from JsonObject by key.
    public bool AsObjectGetBool(string key);
    
    /// Get the null from JsonObject by key.  
    public bool AsObjectGetIsNull(string key);
    • JsonValue is JsonArray.
    /// Get the JsonArray, that is JsonValue list.
    public List<JsonValue> AsArray();
    
    /// Get the JsonValue from JsonArray at index.
    public JsonValue AsArrayGet(int index);
    
    /// Get the JsonObject from JsonArray at index.
    public Dictionary<string, JsonValue> AsArrayGetObject(int index);
    
    /// Get the JsonArray from JsonArray at index.
    public List<JsonValue> AsArrayGetArray(int index);
    
    /// Get the string from JsonArray at index. 
    public string AsArrayGetString(int index);
    
    /// Get the float from JsonArray at index.
    public float AsArrayGetFloat(int index);
    
    /// Get the int from JsonArray at index.
    public int AsArrayGetInt(int index);
    
    /// Get the bool from JsonArray at index.
    public bool AsArrayGetBool(int index);
    
    /// Get the null from JsonArray at index.
    public bool AsArrayGetIsNull(int index);  
  • For C

    • Parse Json string.
    JsonValue* value = AJson->Parse(jsonString);
    • Free any JsonValue memory.
    AJson->Destroy(JsonValue* jsonValue);
    • JsonValue is JsonObject.
    bool        (*GetBool)  (JsonObject* object, const char* key, bool  defaultValue);
    int         (*GetInt)   (JsonObject* object, const char* key, int   defaultValue);
    float       (*GetFloat) (JsonObject* object, const char* key, float defaultValue);
    
    char*       (*GetString)(JsonObject* object, const char* key, const char* defaultValue);
    JsonObject* (*GetObject)(JsonObject* object, const char* key);
    JsonArray*  (*GetArray) (JsonObject* object, const char* key);
    • JsonValue is JsonArray.
    bool        (*GetBool)  (JsonArray* array, int index);
    int         (*GetInt)   (JsonArray* array, int index);
    float       (*GetFloat) (JsonArray* array, int index);
    
    char*       (*GetString)(JsonArray* array, int index);
    JsonObject* (*GetObject)(JsonArray* array, int index);
    JsonArray*  (*GetArray) (JsonArray* array, int index);

How was born

The original implementation and core algorithm came from the Json.h of the pure C game engine Mojoc.

Support

If the source code is useful for you, maybe you could buy me a coffee via Paypal-0.99

mojojson's People

Contributors

scottcgi avatar

Watchers

James Cloos 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.