Skip to content
Open
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
316 changes: 197 additions & 119 deletions CMakeLists.txt

Large diffs are not rendered by default.

53 changes: 44 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
# jsonata-cpp
# Qjsonata

[![Build Status][github-actions-shield]][github-actions-link]

[github-actions-shield]: https://github.com/rayokota/jsonata-cpp/actions/workflows/cmake-multi-platform.yml/badge.svg?branch=master
[github-actions-link]: https://github.com/rayokota/jsonata-cpp/actions

C++ implementation of JSONata.
C++ implementation of JSONata with support for abstract backend.

This is a C++ port of the [JSONata reference implementation](https://github.com/jsonata-js/jsonata),
and also borrows from the [Dashjoin Java port](https://github.com/dashjoin/jsonata-java).

This implementation supports 100% of the language features of JSONata, using [nlohmann_json](https://github.com/nlohmann/json).
The JSONata documentation can be found [here](https://jsonata.org).
QJsonata it is based on the jsonata-cpp port.

This implementation supports 100% of the language features of JSONata, with the exception that the ordering of keys of objects depend on the backend used.
When using nlohmann - which has support for unordered json objects, ordering is as read or generated. When using a backend like QJson which only supports sorted objects, objects are sorted when readed (parsed) or printed (dumped). This 'breaks' some tests as the expected result objects converted to string sometimes do not match the query result converted to string, although the content is compatible as a json object.

In fact QJson supports any 'backend' that supports a certain API, whether it is a json object or not (e.g. a QVariant is also valid). The API is defined
using a c++ context (isCompatible<T>).

The JSONata documentation can be found [here](https://jsonata.org).

## Installation

Expand All @@ -22,19 +28,19 @@ Installation uses CMake as follows:
include(FetchContent)

FetchContent_Declare(
jsonata
URL https://github.com/rayokota/jsonata-cpp/archive/refs/tags/v0.1.0.zip
qjsonata
URL https://github.com/u19809/QJsonata/archive/refs/tags/v0.1.0.zip
URL_HASH SHA256=3ee1798f28a29d36ebbb273853979926716a384e4d491a6bd408e1f6de51760d # Optional
)
FetchContent_MakeAvailable(jsonata)
FetchContent_MakeAvailable(qjsonata)

# Use the library
target_link_libraries(your_target jsonata::jsonata)
target_link_libraries(your_target qjsonata::qjsonata)
```

## Getting Started

A very simple start:
A very simple start (with nlohmann as backend:

```
#include <iostream>
Expand Down Expand Up @@ -63,6 +69,35 @@ int main() {
return 0;
}
```
the same test with QJson as backend

```
#include <iostream>
#include <jsonata/Jsonata.h>
#include <nlohmann/json.hpp>

int main() {
// Create the JSON data
auto data = QJsonValue::fromJson(R"({
"example": [
{"value": 4},
{"value": 7},
{"value": 13}
]
})");

// Create the JSONata expression
jsonata::Jsonata expr("$sum(example.value)");

// Evaluate the expression with the data
auto result = expr.evaluate(data);

// Print the result
std::cout << "Result: " << result << std::endl;

return 0;
}
```

## Running Tests

Expand Down
10 changes: 5 additions & 5 deletions include/jsonata/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@

#include <any>
#include <functional>
#include <nlohmann/json.hpp>
#include <optional>
#include <regex>
#include <sstream>
#include <string>
#include <vector>

#include "Utils.h"
#include "ordered_map.h"

namespace jsonata {

Expand Down Expand Up @@ -133,7 +133,7 @@ class Functions {
const std::string& char_ = " ");
static std::optional<std::string> formatNumber(
double value, const std::string& picture,
const nlohmann::ordered_map<std::string, std::any>& options = {});
const jsonata::ordered_map<std::string, std::any>& options = {});
static std::any shuffle(const Utils::JList& args);
static std::any lookup(const std::any& input, const std::string& key);
static void error(const std::string& message);
Expand Down Expand Up @@ -195,7 +195,7 @@ class Functions {
: implementation(impl), signature(sig) {}
};

static nlohmann::ordered_map<std::string, FunctionEntry>
static jsonata::ordered_map<std::string, FunctionEntry>
getFunctionRegistry();
static std::any applyFunction(const std::string& name,
const Utils::JList& args);
Expand Down Expand Up @@ -233,7 +233,7 @@ class Functions {
static std::string safeReplaceAllFn(const std::string& str,
const std::regex& pattern,
const std::any& func);
static nlohmann::ordered_map<std::string, std::any> toJsonataMatch(
static jsonata::ordered_map<std::string, std::any> toJsonataMatch(
const std::smatch& match);
static std::string encodeURI(const std::string& uri);
static std::string leftPad(const std::string& str, int64_t size,
Expand All @@ -257,7 +257,7 @@ class Functions {
};

static FormatSymbols processOptionsArg(
const nlohmann::ordered_map<std::string, std::any>& options);
const jsonata::ordered_map<std::string, std::any>& options);
static std::string getFormattingCharacter(const std::string& value,
const std::string& propertyName,
bool isChar);
Expand Down
3 changes: 0 additions & 3 deletions include/jsonata/JException.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@
#pragma once

#include <any>
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>
#include <vector>

// Include Utils for JList
#include "Utils.h"

Expand Down
Loading