The JSON library is a library for processing JSON character strings written in JSON format.

This library has the following changes to Jansson Ver.2.14 under the MIT license so that it can be linked to the OPH-5000i user application.

  • Added logs. See Variables for displaying logs.
  • Changed some error messages in json_load_file () in the load.c file from strerror (errno) to errno.
  • Changed buf_to_uint32() in hashtable_seed.c file to comment because it is unused.
  • Changed it to a comment because time.h is not supported.
  • Changed the search order when including jansson_config.h and jansson_private_config.h.
  • Added JANSSON_MODIFY_VERSION define for version control for customization changes.
  • See the provided header file for more information on jansson_config.h.
  • See below for details on jansson_private_config.h to use when creating the library.
jansson_private_config.h
#define HAVE_SYS_TIME_H 1
#define HAVE_SYS_TYPES_H 1
#define HAVE_GETTIMEOFDAY 1
#define HAVE_STDINT_H 1
#define INITIAL_HASHTABLE_ORDER 3

The API hasn't changed from the original Jansson library, so see the Jansson API reference for API details.

The JSON library defines the following data types:

Data typeExplanation
ObjectA JSON object is a dictionary of key-value pairs.
ArrayA JSON array is an ordered collection of other JSON values.
StringJansson uses UTF-8 as the character encoding.
NumberJSON numbers are integer and real values.
Boolean valueJSON boolean values are true and false values.
nullJSON null is a null value.

The outline of the JSON library is as follows.



Convert JSON string to JSON

The json_loads function decodes the JSON string input and returns the array or object it contains. Returns NULL in case of error. In this case, the error is populated with information about the error.


Returns the JSON content as a string

Use the json_dumps function to return the JSON content as a string.



Save the JSON contents to a file

Use the json_dump_file function to output the JSON contents to a file. Output UTF-8.

This function does not support output in encodings other than UTF-8.



Release JSON reference count

As soon as the value is no longer needed, json_decref function should be called to release the reference.



Set the JSON value

Use the json_pack function to create a new JSON value according to the format string fmt.

Below is an excerpt from the Jansson API Reference.

/* Build an empty JSON object */
json_pack("{}");

/* Build the JSON object {"foo": 42, "bar": 7} */
json_pack("{sisi}", "foo", 42, "bar", 7);

/* Like above, ':', ',' and whitespace are ignored */
json_pack("{s:i, s:i}", "foo", 42, "bar", 7);

/* Build the JSON array [[1, 2], {"cool": true}] */
json_pack("[[i,i],{s:b}]", 1, 2, "cool", 1);

/* Build a string from a non-null terminated buffer */
char buffer[4] = { 't', 'e', 's', 't' };
json_pack("s#", buffer, 4);

/* Concatenate strings together to build the JSON string "foobarbaz" */
json_pack("s++", "foo", "bar", "baz");

/* Create an empty object or array when optional members are missing */
json_pack("{s:s*,s:o*,s:O*}", "foo", NULL, "bar", NULL, "baz", NULL);
json_pack("[s*,o*,O*]", NULL, NULL, NULL);

You can use the following to set them individually without using the format.

To set the value of the object by the key value, use the json_object_set_new function.

To add a value to the end of the array, use the json_array_append_new function.



Get the JSON value

Use the json_unpack function to create a new JSON value according to the format string fmt.

Below is an excerpt from the Jansson API Reference.

/* root is the JSON integer 42 */
int myint;
json_unpack(root, "i", &myint);
assert(myint == 42);

/* root is the JSON object {"foo": "bar", "quux": true} */
const char* str;
int boolean;
json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean);
assert(strcmp(str, "bar") == 0 && boolean == 1);

You can use the following to set them individually without using the format.

To get a new JSON object, use the json_object function.

To iterate over every key / value pair and get the value, use the json_object_foreach_safe function.

To get the number of elements in an object, use the json_object_size function.

To get a new JSON array, use the json_array function.

To iterate over all the elements of the array and get the values, use the json_array_foreach function.

To get the number of elements in an array, use the json_array_size function.

To get a new JSON string, use the json_string function.

To get the null-terminated UTF-8 encoded string for the relevant value of the string, use the json_string_value function.

To get a new JSON integer, use the json_integer function.

To get the associated value of an integer, use the json_integer_value function.

To get a new JSON real number, use the json_real function.

To get the relevant value of a real number, use the json_real_value function.

To get the true value of JSON, use the json_true function.

To get the JSON false value, use the json_false function.

To get the JSON null value, use the json_null function.



Determine the type of JSON value

To get the type of JSON value, use the json_typeof function.

To determine the JSON object, use the json_is_object function.

To determine the JSON array, use the json_is_array function.

To determine the JSON string, use the json_is_string function.

To determine the JSON integer value, use the json_is_integer function.

To determine the JSON real value, use the json_is_real function.

To determine the JSON boolean value true, use the json_is_true function.

To determine the JSON boolean value false, use the json_is_false function.

To determine JSON null, use the json_is_null function.



LICENSE

Jansson is the MIT license. Below is the license information.

Copyright (c) 2009-2020 Petri Lehtinen <petri@digip.org>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


Last updated: 2022/01/26