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