Skip to content

Implemented a JSON parser. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools"
"C_Cpp.default.configurationProvider": "vector-of-bool.cmake-tools",
"files.associations": {
"inputstream.h": "c",
"type_traits": "c"
}
}
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ set(
${PROJECT_SOURCE_DIR}/source/jtk/core/StringBuilder.c
${PROJECT_SOURCE_DIR}/source/jtk/core/System.c

${PROJECT_SOURCE_DIR}/source/jtk/support/json.c

# Input/Output Module

${PROJECT_SOURCE_DIR}/source/jtk/io/DataInputStream.c
Expand Down
86 changes: 86 additions & 0 deletions include/jtk/support/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2017-2020 Samuel Rowe
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// Tuesday, June 23 2020

#ifndef JTK_SUPPORT_JSON_H
#define JTK_SUPPORT_JSON_H

#include <jtk/Configuration.h>
#include <jtk/collection/list/ArrayList.h>
#include <jtk/collection/map/HashMap.h>

struct jtk_JsonError_t {

};

typedef struct jtk_JsonError_t jtk_JsonError_t;

enum jtk_JsonValueType_t {
JTK_JSON_VALUE_NULL,
JTK_JSON_VALUE_BOOLEAN,
JTK_JSON_VALUE_NUMBER,
JTK_JSON_VALUE_STRING,
JTK_JSON_VALUE_ARRAY,
JTK_JSON_VALUE_OBJECT
};

typedef enum jtk_JsonValueType_t jtk_JsonValueType_t;

struct jtk_JsonValue_t {
jtk_JsonValueType_t type;
union {
bool boolean;
double number;
struct {
int32_t length;
uint8_t* bytes;
} string;
jtk_ArrayList_t* array;
jtk_HashMap_t* object;
};
};

typedef struct jtk_JsonValue_t jtk_JsonValue_t;

jtk_JsonValue_t* jtk_JsonValue_forObject();
jtk_JsonValue_t* jtk_JsonValue_forArray();
jtk_JsonValue_t* jtk_JsonValue_forString(const uint8_t* text, int32_t length);
jtk_JsonValue_t* jtk_JsonValue_forNumber(const uint8_t* text, int32_t length);
jtk_JsonValue_t* jtk_JsonValue_forTrue();
jtk_JsonValue_t* jtk_JsonValue_forFalse();
void jtk_JsonValue_delete(jtk_JsonValue_t* value);

jtk_JsonValue_t* jtk_parseJson(const uint8_t* sequence, int32_t size, jtk_JsonError_t* error);

/**
* Sometimes you may want to generate JSON instead of parsing it. In such cases,
* you can manually create `jtk_JsonValue_t` objects to create your data and
* invoke `jtk_toJson()`. It will convert the objects to a JSON string.
*
* For example, consider in a text editor application you store all the user
* preferences in a JSON file. Therefore, you will load that file during application
* startup. The preferences will be stored in a `jtk_JsonValue_t` object. When
* your user updates the preferences from your text editor, you need to save it
* to the JSON file. Instead of manually constructing a JSON string, you can just
* edit the `jtk_JsonValue_t` object you initially loaded. When the user wants
* to save the preferences, you simply invoke `jtk_toJson()` and get an equivalent
* string for the user's preferences. You can then write that string to the JSON
* file.
*/
uint8_t* jtk_toJson(jtk_JsonValue_t* value, int32_t* size, bool pretty);

#endif /* JTK_SUPPORT_JSON_H */
1 change: 1 addition & 0 deletions source/jtk/collection/array/ByteArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// Tuesday, March 17, 2020

#include <jtk/collection/array/ByteArray.h>
#include <jtk/collection/array/Arrays.h>

/*******************************************************************************
* ByteArray *
Expand Down
2 changes: 2 additions & 0 deletions source/jtk/io/InputStreamHelper.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <jtk/core/Error.h>
#include <jtk/core/System.h>
#include <jtk/io/InputStreamHelper.h>
#include <jtk/collection/array/Arrays.h>
#include <jtk/collection/array/ByteArray.h>

/*******************************************************************************
* InputStreamHelper *
Expand Down
Loading