ArduinoJson

repository·7.x·Indexed 27 days ago

https://github.com/bblanchon/arduinojson

A high-performance C++ JSON library for Arduino and IoT devices supporting JSON and MessagePack formats. It provides tools for serialization via serializeJson() and deserialization via deserializeJson() using the JsonDocument class. The library includes support for mutable and read-only access to JSON values through JsonVariant and JsonVariantConst, as well as specialized handling for JsonArrayConst.

Tokens
2K
Snippets
2
Records
26
Agent score
92%

What's inside ArduinoJson

  1. Create a GitHub issue for ArduinoJson

    7.x

    When reporting a problem or opening a new issue on GitHub, ensure you provide the following information to receive a fast and helpful response:

    • Good title: A clear, descriptive summary of the issue.
    • Short description: A concise explanation of the problem.
    • Target platform: The hardware or environment you are using.
    • Compiler model and version: The specific compiler being used.
    • MVCE: A Minimal Viable Code Example (a small, self-contained piece of code that reproduces the issue).
    • Compiler output: The full error messages or logs produced by the compiler.
  2. Serialize a JSON document

    7.x

    Use serializeJson() to convert a JsonDocument into a JSON string or to write it to a stream (like Serial).

    JsonDocument doc;
    
    doc["sensor"] = "gps";
    doc["time"]   = 1351824120;
    doc["data"][0] = 48.756080;
    doc["data"][1] = 2.302038;
    
    serializeJson(doc, Serial);
    // This prints: 
    // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
  3. Deserialize a JSON document

    7.x

    Use deserializeJson() to parse a JSON string or stream into a JsonDocument. Once parsed, you can access values using the [] operator with keys or indices.

    const char* json = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
    
    JsonDocument doc;
    deserializeJson(doc, json);
    
    const char* sensor = doc["sensor"];
    long time          = doc["time"];
    double latitude    = doc["data"][0];
    double longitude   = doc["data"][1];
  4. Remove deprecated configuration macros

    7.x

    The following configuration macros have been removed and should be deleted from your project configuration or build flags:

    • ARDUINOJSON_SLOT_OFFSET_SIZE: Use ARDUINOJSON_SLOT_ID_SIZE instead.
    • ARDUINOJSON_ENABLE_STRING_DEDUPLICATION: This feature is now always enabled and the macro is no longer needed.
  5. Upgrade from ArduinoJson 5 to 7

    7.x

    If you are using legacy classes from ArduinoJson 5, you will encounter compilation errors or deprecation warnings. To resolve these, you should migrate to the new API. Specifically, classes like StaticJsonBuffer, DynamicJsonBuffer, JsonBuffer, and RawJson are no longer supported and will trigger a compilation error.

    For a detailed migration guide, visit: https://arduinojson.org/v7/upgrade-from-v5/

  6. Access and modify JSON values with JsonVariant

    7.x

    The JsonVariant class (which inherits from VariantRefBase) provides a mutable interface for interacting with JSON values, objects, and arrays. You can use it to check types, cast values, set new values, and manipulate nested structures.

    Key Operations:

    • Type Checking: Use .is<T>() to check if a value matches a specific type.
    • Casting: Use .as<T>() to cast a value to a specific type.
    • Setting Values: Use .set(value) to copy a value into the variant.
    • Clearing: Use .clear() to set the value to null.
    • Metadata: Use .size() for the number of elements/members and .nesting() for the depth of the value.