Skip to content

Commit

Permalink
Added DeserializationOption::Filter (closes #959)
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Feb 12, 2020
1 parent 42b0d6a commit def6a81
Show file tree
Hide file tree
Showing 16 changed files with 1,091 additions and 65 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,12 @@
ArduinoJson: change log
=======================

HEAD
----

* Added `DeserializationOption::Filter` (issue #959)
* Added example `JsonFilterExample.ino`

v6.14.1 (2020-01-27)
-------

Expand Down
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -17,6 +17,7 @@ ArduinoJson is a C++ JSON library for Arduino and IoT (Internet Of Things).
* [Optionally decodes UTF-16 escape sequences to UTF-8](https://arduinojson.org/v6/api/config/decode_unicode/)
* [Optionally stores links to the input buffer (zero-copy)](https://arduinojson.org/v6/api/json/deserializejson/)
* [Optionally supports comments in the input](https://arduinojson.org/v6/api/config/enable_comments/)
* Optionally filters the input to keep only desired values
* Supports single quotes as a string delimiter
* Compatible with NDJSON and JSON Lines
* [JSON serialization](https://arduinojson.org/v6/api/json/serializejson/)
Expand Down
66 changes: 66 additions & 0 deletions examples/JsonFilterExample/JsonFilterExample.ino
@@ -0,0 +1,66 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2020
// MIT License
//
// This example shows how to use DeserializationOpion::Filter
//
// https://arduinojson.org/v6/example/filter/

#include <ArduinoJson.h>

void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial) continue;

// The huge input: an extract from OpenWeatherMap response
const __FlashStringHelper* input_json = F(
"{\"cod\":\"200\",\"message\":0,\"list\":[{\"dt\":1581498000,\"main\":{"
"\"temp\":3.23,\"feels_like\":-3.63,\"temp_min\":3.23,\"temp_max\":4.62,"
"\"pressure\":1014,\"sea_level\":1014,\"grnd_level\":1010,\"humidity\":"
"58,\"temp_kf\":-1.39},\"weather\":[{\"id\":800,\"main\":\"Clear\","
"\"description\":\"clear "
"sky\",\"icon\":\"01d\"}],\"clouds\":{\"all\":0},\"wind\":{\"speed\":6."
"19,\"deg\":266},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2020-02-12 "
"09:00:00\"},{\"dt\":1581508800,\"main\":{\"temp\":6.09,\"feels_like\":-"
"1.07,\"temp_min\":6.09,\"temp_max\":7.13,\"pressure\":1015,\"sea_"
"level\":1015,\"grnd_level\":1011,\"humidity\":48,\"temp_kf\":-1.04},"
"\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"clear "
"sky\",\"icon\":\"01d\"}],\"clouds\":{\"all\":9},\"wind\":{\"speed\":6."
"64,\"deg\":268},\"sys\":{\"pod\":\"d\"},\"dt_txt\":\"2020-02-12 "
"12:00:00\"}],\"city\":{\"id\":2643743,\"name\":\"London\",\"coord\":{"
"\"lat\":51.5085,\"lon\":-0.1257},\"country\":\"GB\",\"population\":"
"1000000,\"timezone\":0,\"sunrise\":1581492085,\"sunset\":1581527294}}");

// The filter: it contains "true" for each value we want to keep
const __FlashStringHelper* filter_json =
F("{\"list\":[{\"dt\":true,\"main\":{\"temp\":true}]}");

// Create the filter document
StaticJsonDocument<200> filter;
deserializeJson(filter, filter_json);

// Deserialize the document
StaticJsonDocument<400> doc;
deserializeJson(doc, input_json, DeserializationOption::Filter(filter));

// Print the result
serializeJsonPretty(doc, Serial);
}

void loop() {
// not used in this example
}

// See also
// --------
//
// https://arduinojson.org/ contains the documentation for all the functions
// used above. It also includes an FAQ that will help you solve any
// deserialization problem.
//
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
// It begins with a simple example, like the one above, and then adds more
// features like deserializing directly from a file or an HTTP request.
// Learn more at https://arduinojson.org/book/
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
1 change: 1 addition & 0 deletions extras/tests/IntegrationTests/CMakeLists.txt
Expand Up @@ -6,6 +6,7 @@ add_executable(IntegrationTests
gbathree.cpp
issue772.cpp
round_trip.cpp
openweathermap.cpp
)

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
Expand Down
71 changes: 71 additions & 0 deletions extras/tests/IntegrationTests/openweathermap.cpp

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion extras/tests/JsonDeserializer/CMakeLists.txt
Expand Up @@ -6,12 +6,13 @@ add_executable(JsonDeserializerTests
array.cpp
array_static.cpp
DeserializationError.cpp
filter.cpp
incomplete_input.cpp
input_types.cpp
number.cpp
invalid_input.cpp
misc.cpp
nestingLimit.cpp
number.cpp
object.cpp
object_static.cpp
string.cpp
Expand Down

0 comments on commit def6a81

Please sign in to comment.