Skip to content

Commit

Permalink
Add v1.0 files
Browse files Browse the repository at this point in the history
  • Loading branch information
AaravMalani committed Apr 7, 2023
0 parents commit f58436f
Show file tree
Hide file tree
Showing 9 changed files with 665 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
build/
*.rpm
*.deb
.vscode
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 3.20)
project(libjson C)
enable_language(C)
set(SOURCE_DIR src)
set(SOURCES src/json.c)
set(CMAKE_C_STANDARD, 11)
add_library(json STATIC ${SOURCES})
add_library(json_so SHARED ${SOURCES})

set_target_properties(json_so PROPERTIES OUTPUT_NAME json)
set_target_properties(json PROPERTIES C_STANDARD 11)
set_target_properties(json_so PROPERTIES C_STANDARD 11)
install(DIRECTORY include DESTINATION ${CMAKE_INSTALL_PREFIX})
install(TARGETS json json_so DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
if (UNIX AND NOT DO_NOT_LDCONFIG)
install(CODE "execute_process(COMMAND ldconfig)")
endif()

4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## To contribute to libjson
1. Make a [fork](https://github.com/AaravMalani/libjson/fork) of the project
2. Make your changes in your fork
3. Once you're done, open a [pull request](https://github.com/AaravMalani/libjson/compare)
5 changes: 5 additions & 0 deletions DEBIAN_CONTROL
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Package: libjson
Version: 1.0
Architecture: amd64
Maintainer: Aarav Malani <aarav.malani@gmail.com>
Description: A light-weight feature complete JSON parser
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2023 Aarav Malani

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.
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# libjson: A light-weight feature complete JSON parser
## Installation
### Windows
Download the zip file from [the latest release](https://github.com/AaravMalani/libjson/releases/latest) and [add it to
the Visual Studio link arguments](https://learn.microsoft.com/en-us/cpp/build/adding-references-in-visual-cpp-projects?view=msvc-170)
OR
Build from source
```bat
git clone https://github.com/AaravMalani/libjson/
cd libjson
cmake -S . -B build
cd build
cmake --build .
cd ..
rem The .dll and .lib file is in the build directory
rem The include files are in the include directory
```
### Debian-based Linux
Download the [`.deb`](https://github.com/AaravMalani/libjson/releases/latest) file and run
```bash
sudo dpkg -i <file>
```
OR
Build from source

### Red Hat / CentOS / Fedora
Download the [`.rpm`](https://github.com/AaravMalani/libjson/releases/latest) file and

### Linux (From Source)
```bash
git clone https://github.com/AaravMalani/libjson/
cd libjson
cmake -S . -B build
cd build
sudo make install
cd ..
```

## Usage
```c
#include <json.h>

int main(int argc, char** argv){
JSONElement* element = parseJSON("{\"a\":\"b\"}", NULL); // The second argument is for inner recursed calls and can therefore be NULL
printf("%s\n", (char*) getElement(element, "a")->data);
freeJSONElement(element);
element = parseJSON("{\"a\":[1.0, -1.4]}", NULL);
printf("%s\n", (char*) getElement(element, 2, "a", 0)->data);
freeJSONElement(element);

return 0;
}
```
For all the different types, look at `include/json.h`
16 changes: 16 additions & 0 deletions autodeb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
set -e
rm -rf build /tmp/jsonwork/* *.deb *.rpm
cmake -B build -S . -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/tmp/jsonwork/usr/local/ -DDO_NOT_LDCONFIG=true
cd build
make install
cd ..
mkdir -p /tmp/jsonwork/DEBIAN/
cp DEBIAN_CONTROL /tmp/jsonwork/DEBIAN/control
dpkg-deb --build --root-owner-group /tmp/jsonwork
cp /tmp/jsonwork.deb .
VERSION=$(cat DEBIAN_CONTROL | grep Version: | sed -e 's/Version: //')
NAME=$(cat DEBIAN_CONTROL | grep Package: | sed -e 's/Package: //')
ARCH=$(cat DEBIAN_CONTROL | grep Architecture: | sed -e 's/Architecture: //')
mv jsonwork.deb $NAME-$VERSION-$ARCH.deb
fakeroot alien -r $NAME-$VERSION-$ARCH.deb
mv *.rpm $NAME-$VERSION-$ARCH.rpm
41 changes: 41 additions & 0 deletions include/json.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdarg.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef enum
{
// TYPE TYPEOF DETAILS
STRING, // char*
ARRAY, // JSONElement**
BOOLEAN, // uint64_t,
NUMBER, // char*,
OBJECT, // JSONMapElement*
NOTHING // NULL
} JSONType;
typedef struct
{
JSONType type; // The type of the element
size_t length; // Not necessary this will be set
void *data; // The actual data based on above enum
} JSONElement;

typedef struct
{
char *key; // Key is always string
JSONElement *value; // The value JSON element
} JSONMapElement; // A key-value pair for objects

void freeJSONElement(JSONElement* element);
JSONElement *parseJSON(char *data, char **index);
JSONElement *getElement(JSONElement *parent, void *element);
JSONElement *getElements(JSONElement *element, unsigned long long no, ...);

#ifdef __cplusplus
}
#endif
Loading

0 comments on commit f58436f

Please sign in to comment.