mirror of
https://github.com/openwebos/libpbnjson
synced 2026-11-07 11:32:49 -05:00
[GF-5644] Convert HP Copyright to LG Open-webOS-DCO-1.0-Signed-off-by: Maksym Shevchenko <myshevchenko@luxoft.com> Change-Id: Ia489f76f652d207f9fa1b107cd0a19cd153aeccd Reviewed-on: https://g2g.palm.com/2626 Reviewed-by: Build Verification Reviewed-by: Anatolii Sakhnik <anatolii.sakhnik@lge.com> Tested-by: Anatolii Sakhnik <anatolii.sakhnik@lge.com>
100 lines
2.8 KiB
Plaintext
100 lines
2.8 KiB
Plaintext
@@@LICENSE
|
|
|
|
Copyright (c) 2009-2013 LG Electronics, Inc.
|
|
|
|
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.
|
|
|
|
LICENSE@@@
|
|
|
|
For JSON schemas, refer to http://www.json-schema.org for more detailed documentation.
|
|
|
|
To create a JSON object, you probably want something like:
|
|
|
|
-- response.schema
|
|
{
|
|
"type" : "object", /* the top-level response is a JSON object
|
|
"properties" : { /* the keys allowed by
|
|
"errorCode" : { "type" : "integer" }, /* errorCode is a non-floating point number */
|
|
"errorText" : { "type" : "string" } /* errorText is a string value */
|
|
}
|
|
"additionalProperties" : false /* don't allow any other properties to appear in this object */
|
|
}
|
|
|
|
-- example.cpp
|
|
#include <pbnjson.hpp>
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
....
|
|
|
|
namespace pj = pbnjson;
|
|
|
|
....
|
|
{
|
|
pj::JValue myresponse = pj::Object();
|
|
myresponse.put("errorCode", 5); // {"errorCode":5}
|
|
myresponse.put("errorText", "This is an example of a pbnjson object"); // {"errorCode":5,"errorText":"This is an example of a pbnjson object"}
|
|
|
|
pj::JSchemaFile responseSchema("response.schema");
|
|
|
|
pj::JGenerator serializer(NULL); // our schema that we will be using does not have any external references
|
|
std::string serialized;
|
|
|
|
if (!serializer.toString(myresponse, responseSchema, serialized)) {
|
|
// handle error (e.g. generated json would fail to validate & or some other error occured)
|
|
return;
|
|
}
|
|
|
|
std::cout << serialized << std::endl; // write out {"errorCode":5,"errorText"...} to stdout
|
|
}
|
|
|
|
To parse a JSON object, you probably want something like:
|
|
|
|
-- input.schema
|
|
{
|
|
"type" : "object",
|
|
"properties" : {
|
|
"guess" : {
|
|
"type" : "number",
|
|
"minimum" : 1,
|
|
"maximum" : 10
|
|
}
|
|
}
|
|
}
|
|
|
|
-- example.cpp
|
|
#include <pbnjson.hpp>
|
|
|
|
...
|
|
|
|
namespace pj = pbnjson;
|
|
|
|
...
|
|
|
|
{
|
|
std::string input;
|
|
... // input is initialized
|
|
|
|
pj::JSchemaFile inputSchema("input.schema");
|
|
|
|
JDomParser parser(NULL); // our schema that we will be using does not have any external references
|
|
if (!parser.parse(input, inputSchema, NULL)) { // in this example, I don't care about the actual errors that cause parsing to fail.
|
|
// handle error
|
|
return;
|
|
}
|
|
|
|
pj::JValue parsed = parser.getDom(); // parsed is a validly formed JSON object or array and validated against the schema.
|
|
std::cout << parsed["guess"] << std::endl; // this is always guaranteed to print a number between 1 & 10. no additional validation necessary within the code.
|
|
}
|
|
|