as2js: as2js::JSON Class Reference

#include <json.h>

Classes class  JSONValue   Public Types typedef std::shared_ptr< JSONpointer_t   Public Member Functions JSONValue::pointer_t get_value () const  Retrieve the value of the JSON object. More...
  JSONValue::pointer_t load (String const &filename)  Read a JSON value. More...
   operator bool () const   bool output (Output::pointer_t out, String const &header) const  Output this JSON to the specified output. More...
  JSONValue::pointer_t parse (Input::pointer_t in)  Parse a JSON object. More...
  bool save (String const &filename, String const &header) const  Save the JSON in the specified file. More...
  void set_value (JSONValue::pointer_t value)  Set the value of this JSON object. More...
  Private Member Functions JSONValue::pointer_t read_json_value (Node::pointer_t n)  Read only JSON value. More...
  Private Attributes Lexer::pointer_t f_lexer = Lexer::pointer_t()   JSONValue::pointer_t f_value = JSONValue::pointer_t()   Detailed Description

Definition at line 63 of file json.h.

Member Typedef Documentation
typedef std::shared_ptr<JSON> as2js::JSON::pointer_t

Definition at line 66 of file json.h.

Member Function Documentation
JSON::JSONValue::pointer_t as2js::JSON::get_value ( ) const

This function returns the current value of this JSON object. This is the function you need to call after a call to the load() or parse() functions used to read a JSON file from an input stream.

Note that this function gives you the root JSONValue object of the JSON object. You can then read the data or modify it as required. If you make changes, you may later call the save() or output() functions to save the changes to a file or an output stream.

A pointer to the JSONValue of this JSON object.

Definition at line 1171 of file json.cpp.

References f_value.

Referenced by operator bool().

JSON::JSONValue::pointer_t as2js::JSON::load ( String const &  filename)

This function opens a FileInput stream, setups a default Position and then calls parse() to parse the file in a JSON tree.

[in]filenameThe name of a JSON file.

Definition at line 765 of file json.cpp.

References as2js::AS_ERR_NOT_FOUND, as2js::MESSAGE_LEVEL_ERROR, parse(), and as2js::Position::set_filename().

as2js::JSON::operator bool ( ) const inline

Definition at line 139 of file json.h.

References f_value, get_value(), read_json_value(), and set_value().

bool as2js::JSON::output ( Output::pointer_t  out, String const &  header  ) const

This function saves this JSON to the specified output object: out.

If a header is specified (i.e. header is not an empty string) then it gets saved before any JSON data.

The output file is made a UTF-8 text file as the function first saves a BOM in the file. Note that this means you should NOT save anything in that file before calling this function. You may, however, write more data (a footer) on return.

Functions called by this function may generate the cyclic exception. This happens if your JSON tree is cyclic which means that a child element points back to one of its parent.
exception_invalid_dataThis exception is raised in the event the JSON does not have any data to be saved. This happens if you create a JSON object and never load or parse a valid JSON or call the set_value() function.
[in]outThe output stream where the JSON is to be saved. [in]headerA string representing a header. It should be written in a C or C++ comment for the parser to be able to re-read the file seamlessly.
true if the data was successfully written to out.

Definition at line 1113 of file json.cpp.

References f_value, and as2js::String::STRING_BOM.

Referenced by save().

JSON::JSONValue::pointer_t as2js::JSON::parse ( Input::pointer_t  in)

This function is used to read a JSON input stream.

If a recoverable error occurs, the function returns with a JSONValue smart pointer. If errors occur, then a message is created and sent, but as much as possible of the input file is read in.

Note that the resulting value may be a NULL pointer if too much failed.

An empty file is not a valid JSON file. To the minimum you must have:

[in]inThe input stream to be parsed.
A pointer to a JSONValue tree, it may be a NULL pointer.

Definition at line 804 of file json.cpp.

References as2js::AS_ERR_CANNOT_COMPILE, f_lexer, f_value, as2js::MESSAGE_LEVEL_FATAL, as2js::Options::OPTION_JSON, and read_json_value().

Referenced by load().

JSON::JSONValue::pointer_t as2js::JSON::read_json_value ( Node::pointer_t  n) private

This function transform the specified n node in a JSONValue object.

The type of object is defined from the type of node we just received from the lexer.

If the lexer returned a NODE_SUBTRACT, then we assume we are about to read an integer or a floating point. We do that and then calculate the opposite and save the result as a FLOAT64 or INT64 JSONValue.

If the lexer returned a NODE_OPEN_SQUARE_BRACKET then the function enters the mode used to read an array.

If the lexer returned a NODE_OPEN_CURVLY_BRACKET then the function enters the mode used to read an object.

Note that the function is somewhat weak in regard to error handling. If the input is not valid as per as2js JSON documentation, then an error is emitted and the process stops early.

[in]nThe node to be transform in a JSON value.

Definition at line 863 of file json.cpp.

References as2js::AS_ERR_COLON_EXPECTED, as2js::AS_ERR_COMMA_EXPECTED, as2js::AS_ERR_OBJECT_MEMBER_DEFINED_TWICE, as2js::AS_ERR_STRING_EXPECTED, as2js::AS_ERR_UNEXPECTED_EOF, as2js::AS_ERR_UNEXPECTED_TOKEN, f_lexer, as2js::JSON::JSONValue::JSONValue(), as2js::MESSAGE_LEVEL_ERROR, as2js::Node::NODE_ADD, as2js::Node::NODE_CLOSE_CURVLY_BRACKET, as2js::Node::NODE_CLOSE_SQUARE_BRACKET, as2js::Node::NODE_COLON, as2js::Node::NODE_COMMA, as2js::Node::NODE_EOF, as2js::Node::NODE_FALSE, as2js::Node::NODE_FLOAT64, as2js::Node::NODE_INT64, as2js::Node::NODE_NULL, as2js::Node::NODE_OPEN_CURVLY_BRACKET, as2js::Node::NODE_OPEN_SQUARE_BRACKET, as2js::Node::NODE_STRING, as2js::Node::NODE_SUBTRACT, as2js::Node::NODE_TRUE, as2js::Int64::set(), and as2js::Float64::set().

Referenced by operator bool(), and parse().

bool as2js::JSON::save ( String const &  filename, String const &  header  ) const

This function is used to save this JSON in the specified file.

One can also specified a header, in most cases a comment that gives copyright, license information and eventually some information explaining what that file is about.

[in]filenameThe name of the file on disk. [in]headerA header to be saved before the JSON data.
true if the save() succeeded.
See also

Definition at line 1069 of file json.cpp.

References as2js::AS_ERR_CANNOT_COMPILE, as2js::MESSAGE_LEVEL_FATAL, and output().

void as2js::JSON::set_value ( JSONValue::pointer_t  value)

This function is used to define the value of this JSON object. This is used whenever you create a JSON in memory and want to save it on disk or send it to a client.

[in]valueThe JSONValue to save in this JSON object.

Definition at line 1151 of file json.cpp.

References f_value.

Referenced by operator bool().

Member Data Documentation
Lexer::pointer_t as2js::JSON::f_lexer = Lexer::pointer_t() private

Definition at line 147 of file json.h.

Referenced by parse(), and read_json_value().

JSONValue::pointer_t as2js::JSON::f_value = JSONValue::pointer_t() private

Definition at line 148 of file json.h.

Referenced by get_value(), operator bool(), output(), parse(), and set_value().

The documentation for this class was generated from the following files:
  • /home/snapwebsites/snapcpp/contrib/as2js/include/as2js/json.h
  • /home/snapwebsites/snapcpp/contrib/as2js/lib/json.cpp

This document is part of the Snap! Websites Project.

Copyright by Made to Order Software Corp.

Syndicate content

Snap! Websites
An Open Source CMS System in C++

Contact Us Directly