Skip to content

Commit

Permalink
Merge pull request RIOT-OS#20129 from bergzand/pr/mjson/initial
Browse files Browse the repository at this point in the history
mjson: Initial include of package
  • Loading branch information
MrKevinWeiss authored Dec 18, 2023
2 parents 59573a8 + 18c1548 commit 5ba18df
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ rsource "micro-ecc/Kconfig"
rsource "microcoap/Kconfig"
rsource "micropython/Kconfig"
rsource "minmea/Kconfig"
rsource "mjson/Kconfig"
rsource "monocypher/Kconfig"
rsource "mynewt-core/Kconfig"
rsource "nanocbor/Kconfig"
Expand Down
9 changes: 9 additions & 0 deletions pkg/mjson/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2023 Koen Zandberg
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

config PACKAGE_MJSON
bool "mjson"
depends on TEST_KCONFIG
10 changes: 10 additions & 0 deletions pkg/mjson/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PKG_NAME=mjson
PKG_URL=https://github.com/cesanta/mjson
# v1.2.7
PKG_VERSION=032a2eaca1a989e56d88218f2a3cb91a68afebe8
PKG_LICENSE=MIT

include $(RIOTBASE)/pkg/pkg.mk

all:
$(QQ)"$(MAKE)" -C $(PKG_SOURCE_DIR)/src -f $(CURDIR)/$(PKG_NAME).mk
1 change: 1 addition & 0 deletions pkg/mjson/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
INCLUDES += -I$(PKGDIRBASE)/mjson/src
16 changes: 16 additions & 0 deletions pkg/mjson/doc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @defgroup pkg_mjson mjson
* @ingroup pkg
* @brief mjson - a JSON parser + emitter + JSON-RPC engine
*
* # Introduction
*
* mjson is a small, embedded-friendly JSON parser, emitter and JSON-RPC
* engine.
*
* # License
*
* Licensed under MIT.
*
* @see https://github.com/cesanta/mjson
*/
7 changes: 7 additions & 0 deletions pkg/mjson/mjson.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
MODULE = mjson

# The mjson_merge function requires an alloca implementation, disabled to
# prevent stack memory allocations
CFLAGS += -DMJSON_ENABLE_MERGE=0

include $(RIOTBASE)/Makefile.base
6 changes: 6 additions & 0 deletions tests/pkg/mjson/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
include ../Makefile.pkg_common

USEMODULE += embunit
USEPKG += mjson

include $(RIOTBASE)/Makefile.include
6 changes: 6 additions & 0 deletions tests/pkg/mjson/Makefile.ci
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BOARD_INSUFFICIENT_MEMORY := \
atmega8 \
nucleo-l011k4 \
samd10-xmini \
stm32f030f4-demo \
#
2 changes: 2 additions & 0 deletions tests/pkg/mjson/app.config.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CONFIG_MODULE_EMBUNIT=y
CONFIG_PACKAGE_MJSON=y
80 changes: 80 additions & 0 deletions tests/pkg/mjson/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2023 Koen Zandberg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup tests
* @{
*
* @file
* @brief Test application for mjson
*
* @author Koen Zandberg <koen@bergzand.net>
*
* @}
*/

#include <stdio.h>
#include "embUnit.h"
#include "mjson.h"

void test_mjson_encode(void)
{
/* Print into a statically allocated buffer */
char buf[100];
mjson_snprintf(buf, sizeof(buf), "{%Q:%d}", "a", 123);

static const char expected[] = "{\"a\":123}";

TEST_ASSERT_EQUAL_INT(0, strcmp(expected, buf));
}

void test_mjson_decode(void)
{
static const char input[] = "{\"a\":1,\"b\":[2,false]}"; /* {"a":1,"b":[2,false]} */

double dval = 0;
int res = mjson_get_number(input, strlen(input), "$.a", &dval);
TEST_ASSERT_EQUAL_INT(1, res);
TEST_ASSERT_EQUAL_INT(1, dval);

const char *buf;
int buf_len;
res = mjson_find(input, strlen(input), "$.b", &buf, &buf_len);
TEST_ASSERT_EQUAL_INT(91, res);
TEST_ASSERT_EQUAL_INT(0, memcmp(buf, "[2,false]", buf_len));

res = mjson_get_number(input, strlen(input), "$.b[0]", &dval);
TEST_ASSERT_EQUAL_INT(1, res);
TEST_ASSERT_EQUAL_INT(2, dval);

int ival = 0;
res = mjson_get_bool(input, strlen(input), "$.b[1]", &ival);
TEST_ASSERT_EQUAL_INT(1, res);
TEST_ASSERT_EQUAL_INT(0, ival);

}

Test *tests_mjson(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_mjson_encode),
new_TestFixture(test_mjson_decode),
};

EMB_UNIT_TESTCALLER(mjson_tests, NULL, NULL, fixtures);
return (Test*)&mjson_tests;
}

int main(void)
{
TESTS_START();
TESTS_RUN(tests_mjson());
TESTS_END();

return 0;
}
14 changes: 14 additions & 0 deletions tests/pkg/mjson/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env python3

# Copyright (C) 2023 Koen Zandberg
#
# This file is subject to the terms and conditions of the GNU Lesser
# General Public License v2.1. See the file LICENSE in the top level
# directory for more details.

import sys
from testrunner import run_check_unittests


if __name__ == "__main__":
sys.exit(run_check_unittests())

0 comments on commit 5ba18df

Please sign in to comment.