Merge pull request #83 from eadrkir/master
[deliverable/titan.core.git] / core / JSON.hh
CommitLineData
d44e3c4f 1/******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Baranyi, Botond
11 *
12 ******************************************************************************/
970ed795
EL
13#ifndef JSON_HH_
14#define JSON_HH_
15
16#include "Types.h"
17
18/** Descriptor for JSON encoding/decoding during runtime */
19struct TTCN_JSONdescriptor_t
20{
21 /** Encoding only.
22 * true : use the null literal to encode omitted fields in records or sets
3abe9331 23 * example: { "field1" : value1, "field2" : null, "field3" : value3 }
970ed795 24 * false : skip both the field name and the value if a field is omitted
3abe9331 25 * example: { "field1" : value1, "field3" : value3 }
970ed795
EL
26 * The decoder will always accept both variants. */
27 boolean omit_as_null;
28
29 /** An alias for the name of the field (in a record, set or union).
30 * Encoding: this alias will appear instead of the name of the field
3abe9331 31 * Decoding: the decoder will look for this alias instead of the field's real name */
970ed795
EL
32 const char* alias;
33
34 /** If set, the union will be encoded as a JSON value instead of a JSON object
35 * with one name-value pair.
36 * Since the field name is no longer present, the decoder will determine the
37 * selected field based on the type of the value. The first field (in the order
38 * of declaration) that can successfully decode the value will be the selected one. */
39 boolean as_value;
40
41 /** Decoding only.
3abe9331 42 * Fields that don't appear in the JSON code will decode this value instead. */
970ed795 43 const char* default_value;
3abe9331 44
45 /** If set, encodes unbound fields of records and sets as null and inserts a
46 * meta info field into the JSON object specifying that the field is unbound.
47 * The decoder sets the field to unbound if the meta info field is present and
48 * the field's value in the JSON code is either null or a valid value for that
49 * field.
feade998 50 * Example: { "field1" : null, "metainfo field1" : "unbound" }
51 *
52 * Also usable on record of/set of/array types to indicate that an element is
53 * unbound. Unbound elements are encoded as a JSON object containing one
54 * metainfo member. The decoder sets the element to unbound if the object
55 * with the meta information is found.
56 * Example: [ value1, value2, { "metainfo []" : "unbound" }, value3 ] */
3abe9331 57 boolean metainfo_unbound;
970ed795
EL
58};
59
3abe9331 60/** This macro makes sure that coding errors will only be displayed if the silent
61 * flag is not set. */
970ed795
EL
62#define JSON_ERROR if(!p_silent) TTCN_EncDec_ErrorContext::error
63
64// JSON descriptors for base types
65extern const TTCN_JSONdescriptor_t INTEGER_json_;
66extern const TTCN_JSONdescriptor_t FLOAT_json_;
67extern const TTCN_JSONdescriptor_t BOOLEAN_json_;
68extern const TTCN_JSONdescriptor_t BITSTRING_json_;
69extern const TTCN_JSONdescriptor_t HEXSTRING_json_;
70extern const TTCN_JSONdescriptor_t OCTETSTRING_json_;
71extern const TTCN_JSONdescriptor_t CHARSTRING_json_;
72extern const TTCN_JSONdescriptor_t UNIVERSAL_CHARSTRING_json_;
73extern const TTCN_JSONdescriptor_t VERDICTTYPE_json_;
74extern const TTCN_JSONdescriptor_t GeneralString_json_;
75extern const TTCN_JSONdescriptor_t NumericString_json_;
76extern const TTCN_JSONdescriptor_t UTF8String_json_;
77extern const TTCN_JSONdescriptor_t PrintableString_json_;
78extern const TTCN_JSONdescriptor_t UniversalString_json_;
79extern const TTCN_JSONdescriptor_t BMPString_json_;
80extern const TTCN_JSONdescriptor_t GraphicString_json_;
81extern const TTCN_JSONdescriptor_t IA5String_json_;
82extern const TTCN_JSONdescriptor_t TeletexString_json_;
83extern const TTCN_JSONdescriptor_t VideotexString_json_;
84extern const TTCN_JSONdescriptor_t VisibleString_json_;
af710487 85extern const TTCN_JSONdescriptor_t ASN_NULL_json_;
86extern const TTCN_JSONdescriptor_t OBJID_json_;
87extern const TTCN_JSONdescriptor_t ASN_ROID_json_;
88extern const TTCN_JSONdescriptor_t ASN_ANY_json_;
89extern const TTCN_JSONdescriptor_t ENUMERATED_json_;
970ed795
EL
90
91/** JSON decoder error codes */
92enum json_decode_error {
93 /** An unexpected JSON token was extracted. The token might still be valid and
94 * useful for the caller structured type. */
95 JSON_ERROR_INVALID_TOKEN = -1,
96 /** The JSON tokeniser couldn't extract a valid token (JSON_TOKEN_ERROR) or the
97 * format of the data extracted is invalid. In either case, this is a fatal
98 * error and the decoding cannot continue.
3abe9331 99 * @note This error code is always preceeded by a decoding error, if the
970ed795
EL
100 * caller receives this code, it means that decoding error behavior is (at least
101 * partially) set to warnings. */
102 JSON_ERROR_FATAL = -2
103};
104
3abe9331 105/** JSON meta info states during decoding */
106enum json_metainfo_t {
107 /** The field does not have meta info enabled */
108 JSON_METAINFO_NOT_APPLICABLE,
109 /** Initial state if meta info is enabled for the field */
110 JSON_METAINFO_NONE,
111 /** The field's value is set to null, but no meta info was received for the field yet */
112 JSON_METAINFO_NEEDED,
113 /** Meta info received: the field is unbound */
114 JSON_METAINFO_UNBOUND
115};
116
970ed795
EL
117// JSON decoding error messages
118#define JSON_DEC_BAD_TOKEN_ERROR "Failed to extract valid token, invalid JSON format%s"
119#define JSON_DEC_FORMAT_ERROR "Invalid JSON %s format, expecting %s value"
120#define JSON_DEC_NAME_TOKEN_ERROR "Invalid JSON token, expecting JSON field name"
121#define JSON_DEC_OBJECT_END_TOKEN_ERROR "Invalid JSON token, expecting JSON name-value pair or object end mark%s"
122#define JSON_DEC_REC_OF_END_TOKEN_ERROR "Invalid JSON token, expecting JSON value or array end mark%s"
123#define JSON_DEC_ARRAY_ELEM_TOKEN_ERROR "Invalid JSON token, expecting %d more JSON value%s"
124#define JSON_DEC_ARRAY_END_TOKEN_ERROR "Invalid JSON token, expecting JSON array end mark%s"
125#define JSON_DEC_FIELD_TOKEN_ERROR "Invalid JSON token found while decoding field '%s'"
126#define JSON_DEC_INVALID_NAME_ERROR "Invalid field name '%s'"
127#define JSON_DEC_MISSING_FIELD_ERROR "No JSON data found for field '%s'"
128#define JSON_DEC_STATIC_OBJECT_END_TOKEN_ERROR "Invalid JSON token, expecting JSON object end mark%s"
129#define JSON_DEC_AS_VALUE_ERROR "Extracted JSON %s could not be decoded by any field of the union"
3abe9331 130#define JSON_DEC_METAINFO_NAME_ERROR "Meta info provided for non-existent field '%s'"
131#define JSON_DEC_METAINFO_VALUE_ERROR "Invalid meta info for field '%s'"
132#define JSON_DEC_METAINFO_NOT_APPLICABLE "Meta info not applicable to field '%s'"
970ed795
EL
133
134#endif /* JSON_HH_ */
135
This page took 0.030268 seconds and 5 git commands to generate.