Arduino example are now compiling

This commit is contained in:
Benoit Blanchon
2014-11-03 21:29:55 +01:00
parent d9cc259df3
commit 699292b058
5 changed files with 104 additions and 84 deletions

View File

@ -3,17 +3,13 @@
* Benoit Blanchon 2014 - MIT License * Benoit Blanchon 2014 - MIT License
*/ */
#include <JsonGenerator.h> #include <ArduinoJson.h>
using namespace ArduinoJson::Generator; using namespace ArduinoJson::Internals;
void setup() void setup() {
{
Serial.begin(9600); Serial.begin(9600);
JsonObject<1> json;
json["key"] = "value";
IndentedPrint serial(Serial); IndentedPrint serial(Serial);
serial.setTabSize(4); serial.setTabSize(4);
@ -24,20 +20,11 @@ void setup()
serial.indent(); serial.indent();
serial.println("This is at indentation 2"); serial.println("This is at indentation 2");
serial.println("You can print JSON here, as usual:");
serial.println(json);
serial.println();
serial.println("But you can also prettyPrint JSON here:");
json.prettyPrintTo(serial);
serial.println();
serial.unindent(); serial.unindent();
serial.unindent(); serial.unindent();
serial.println("This is back at indentation 0"); serial.println("This is back at indentation 0");
} }
void loop() void loop() {
{ // not used in this example
} }

View File

@ -1,32 +1,43 @@
/* // Copyright Benoit Blanchon 2014
* Arduino JSON library - Generator example // MIT License
* Benoit Blanchon 2014 - MIT License //
*/ // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <JsonGenerator.h> #include <ArduinoJson.h>
using namespace ArduinoJson::Generator; void setup() {
void setup()
{
Serial.begin(9600); Serial.begin(9600);
JsonArray<2> array; StaticJsonBuffer<200> jsonBuffer;
array.add<6>(48.756080); // 6 is the number of decimals to print
array.add<6>(2.302038); // if not specified, 2 digits are printed
JsonObject<3> root; JsonArray& array = jsonBuffer.createArray();
array.add(48.756080, 6); // 6 is the number of decimals to print
array.add(2.302038, 6); // if not specified, 2 digits are printed
JsonObject& root = jsonBuffer.createObject();
root["sensor"] = "gps"; root["sensor"] = "gps";
root["time"] = 1351824120; root["time"] = 1351824120;
root["data"] = array; root["data"] = array;
Serial.print(root); // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} root.printTo(Serial);
// This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
Serial.println(); Serial.println();
root.prettyPrintTo(Serial); // same string indented
root.prettyPrintTo(Serial);
// This prints:
// {
// "sensor": "gps",
// "time": 1351824120,
// "data": [
// 48.756080,
// 2.302038
// ]
// }
} }
void loop() void loop() {
{ // not used in this example
} }

View File

@ -1,29 +1,27 @@
/* // Copyright Benoit Blanchon 2014
* Arduino JSON library - Parser Example // MIT License
* Benoit Blanchon 2014 - MIT License //
*/ // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#include <JsonParser.h> #include <ArduinoJson.h>
using namespace ArduinoJson::Parser; void setup() {
void setup()
{
Serial.begin(9600); Serial.begin(9600);
char json [] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; StaticJsonBuffer<200> jsonBuffer;
JsonParser<16> parser; char json[] =
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
JsonObject root = parser.parse(json); JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) if (!root.success()) {
{ Serial.println("parseObject() failed");
Serial.println("JsonParser.parse() failed");
return; return;
} }
char* sensor = root["sensor"]; const char* sensor = root["sensor"];
long time = root["time"]; long time = root["time"];
double latitude = root["data"][0]; double latitude = root["data"][0];
double longitude = root["data"][1]; double longitude = root["data"][1];
@ -34,7 +32,6 @@ void setup()
Serial.println(longitude, 6); Serial.println(longitude, 6);
} }
void loop() void loop() {
{ // not used in this example
} }

8
library.properties Normal file
View File

@ -0,0 +1,8 @@
name=ArduinoJson
version=4.0
author=Benoit Blanchon <http://blog.benoitblanchon.fr/>
maintainer=Benoit Blanchon <http://blog.benoitblanchon.fr/>
sentence=An efficient and elegant JSON library for Arduino
paragraph=Supports JSON parsing and formatting. Uses fixed memory allocation.
url=https://github.com/bblanchon/ArduinoJson
architectures=*

17
src/ArduinoJson.h Normal file
View File

@ -0,0 +1,17 @@
// Copyright Benoit Blanchon 2014
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// About this file
// ---------------
// This file is here to please the Arduino IDE. It must be present in the src/
// for the IDE to find it. Feel free to ignore this file if your working in
// another environment
#include "../include/ArduinoJson/StaticJsonBuffer.hpp"
#include "../include/ArduinoJson/JsonArray.hpp"
#include "../include/ArduinoJson/JsonObject.hpp"
using namespace ArduinoJson;