open62541 1.3.12
Open source implementation of OPC UA
Loading...
Searching...
No Matches
ua_types_encoding_json.h
Go to the documentation of this file.
1/** This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 *
5 * Copyright 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
6 * Copyright 2018 (c) Fraunhofer IOSB (Author: Lukas Meling)
7 */
8
9#ifndef UA_TYPES_ENCODING_JSON_H_
10#define UA_TYPES_ENCODING_JSON_H_
11
12#include <open62541/types.h>
13
16#include "ua_util_internal.h"
17
18#include "../deps/jsmn/jsmn.h"
19
21
22#define UA_JSON_MAXTOKENCOUNT 2048
23
24/** Returns the number of bytes the value src takes in json encoding. Returns
25 * zero if an error occurs. */
26size_t
27UA_calcSizeJsonInternal(const void *src, const UA_DataType *type,
28 const UA_String *namespaces, size_t namespaceSize,
29 const UA_String *serverUris, size_t serverUriSize,
30 UA_Boolean useReversible);
31
32/** Encodes the scalar value described by type to json encoding.
33 *
34 * @param src The value. Must not be NULL.
35 * @param type The value type. Must not be NULL.
36 * @param bufPos Points to a pointer to the current position in the encoding
37 * buffer. Must not be NULL.
38 * @param bufEnd Points to a pointer to the end of the encoding buffer (encoding
39 * always stops before *buf_end). Must not be NULL.
40 * @param namespaces An array of namespaces
41 * @param namespaceSize The size of the namespaces array
42 * @param serverUris An array of serverUris
43 * @param serverUriSize The size of the serverUris array
44 * @param useReversible preserve datatypes in json encoding
45 * @return Returns a statuscode whether encoding succeeded. */
47UA_encodeJsonInternal(const void *src, const UA_DataType *type, uint8_t **bufPos,
48 const uint8_t **bufEnd, const UA_String *namespaces,
49 size_t namespaceSize, const UA_String *serverUris,
50 size_t serverUriSize,
51 UA_Boolean useReversible);
52
53/** Interal Definitions
54 *
55 * For future by the PubSub encoding */
56
57#define UA_JSON_ENCODING_MAX_RECURSION 100
58typedef struct {
59 uint8_t *pos;
60 const uint8_t *end;
61
62 uint16_t depth; /* How often did we en-/decoding recurse? */
65 UA_Boolean calcOnly; /* Only compute the length of the decoding */
66
69
72} CtxJson;
73
76 const void *value, const UA_DataType *type);
78
80UA_StatusCode writeJsonArrElm(CtxJson *ctx, const void *value,
81 const UA_DataType *type);
83
84UA_StatusCode writeJsonKey(CtxJson *ctx, const char* key);
87
88/** The encoding length is returned in ctx->pos */
89static UA_StatusCode
90calcJsonObjStart(CtxJson *ctx) {
91 UA_assert(ctx->calcOnly);
92 return writeJsonObjStart(ctx);
93}
94
95static UA_StatusCode
96calcJsonObjElm(CtxJson *ctx, const char *key,
97 const void *value, const UA_DataType *type) {
98 UA_assert(ctx->calcOnly);
99 return writeJsonObjElm(ctx, key, value, type);
100}
101
102static UA_StatusCode
103calcJsonObjEnd(CtxJson *ctx) {
104 UA_assert(ctx->calcOnly);
105 return writeJsonObjEnd(ctx);
106}
107
108static UA_StatusCode
109calcJsonArrStart(CtxJson *ctx) {
110 UA_assert(ctx->calcOnly);
111 return writeJsonArrStart(ctx);
112}
113
114static UA_StatusCode
115calcJsonArrElm(CtxJson *ctx, const void *value,
116 const UA_DataType *type) {
117 UA_assert(ctx->calcOnly);
118 return writeJsonArrElm(ctx, value, type);
119}
120
121static UA_StatusCode
122calcJsonArrEnd(CtxJson *ctx) {
123 UA_assert(ctx->calcOnly);
124 return writeJsonArrEnd(ctx);
125}
126
127status
128encodeJsonInternal(const void *src, const UA_DataType *type, CtxJson *ctx);
129
130typedef struct {
134
135 /* Additonal data for special cases such as networkmessage/datasetmessage
136 * Currently only used for dataSetWriterIds */
137 size_t numCustom;
138 void * custom;
140
142} ParseCtx;
143
145(*encodeJsonSignature)(const void *src, const UA_DataType *type, CtxJson *ctx);
146
148(*decodeJsonSignature)(void *dst, const UA_DataType *type,
149 CtxJson *ctx, ParseCtx *parseCtx);
150
151/** Map for decoding a Json Object. An array of this is passed to the
152 * decodeFields function. If the key "fieldName" is found in the json object
153 * (mark as found and) decode the value with the "function" and write result
154 * into "fieldPointer" (destination). */
155typedef struct {
156 const char *fieldName;
160 const UA_DataType *type; /* Must be set for values that can be "null". If
161 * the function is not set, decode via the
162 * type->typeKind. */
164
167 DecodeEntry *entries, size_t entryCount);
168
169/** Expose the jump tables and some methods for PubSub JSON decoding */
172
173UA_StatusCode lookAheadForKey(const char* search, CtxJson *ctx,
174 ParseCtx *parseCtx, size_t *resultIndex);
176 const UA_ByteString *src, size_t tokensSize);
177UA_Boolean isJsonNull(const CtxJson *ctx, const ParseCtx *parseCtx);
178
179static
180jsmntype_t getJsmnType(const ParseCtx *parseCtx) {
181 return parseCtx->tokenArray[parseCtx->index].type;
182}
183
185
186#endif /* UA_TYPES_ENCODING_JSON_H_ */
#define UA_assert(ignore)
#define _UA_BEGIN_DECLS
#undef UA_DEBUG_DUMP_PKGS
Definition config.h:89
#define _UA_END_DECLS
Definition config.h:96
jsmntype_t
Definition jsmn.h:11
const UA_String * serverUris
const uint8_t * end
const UA_String * namespaces
UA_Boolean calcOnly
UA_Boolean useReversible
Map for decoding a Json Object.
decodeJsonSignature function
const UA_DataType * type
jsmntok_t * tokenArray
const UA_DataTypeArray * customTypes
Datatype arrays with custom type definitions can be added in a linked list to the client or server co...
Definition types.h:859
jsmntype_t type
Definition jsmn.h:31
int32_t UA_Int32
Definition types.h:51
_UA_BEGIN_DECLS typedef bool UA_Boolean
This Source Code Form is subject to the terms of the Mozilla Public License, v.
Definition types.h:26
uint16_t UA_UInt16
Definition types.h:46
#define UA_DATATYPEKINDS
The DataType "kind" is an internal type classification.
Definition types.h:805
uint32_t UA_StatusCode
Definition types.h:77
status encodeJsonInternal(const void *src, const UA_DataType *type, CtxJson *ctx)
const decodeJsonSignature decodeJsonJumpTable[UA_DATATYPEKINDS]
#define UA_JSON_ENCODING_MAX_RECURSION
Interal Definitions.
const encodeJsonSignature encodeJsonJumpTable[UA_DATATYPEKINDS]
Expose the jump tables and some methods for PubSub JSON decoding.
UA_StatusCode writeJsonObjElm(CtxJson *ctx, const char *key, const void *value, const UA_DataType *type)
UA_StatusCode UA_encodeJsonInternal(const void *src, const UA_DataType *type, uint8_t **bufPos, const uint8_t **bufEnd, const UA_String *namespaces, size_t namespaceSize, const UA_String *serverUris, size_t serverUriSize, UA_Boolean useReversible)
Encodes the scalar value described by type to json encoding.
UA_StatusCode writeJsonArrStart(CtxJson *ctx)
UA_StatusCode writeJsonNull(CtxJson *ctx)
UA_StatusCode writeJsonArrEnd(CtxJson *ctx)
UA_StatusCode(* encodeJsonSignature)(const void *src, const UA_DataType *type, CtxJson *ctx)
UA_Boolean isJsonNull(const CtxJson *ctx, const ParseCtx *parseCtx)
size_t UA_calcSizeJsonInternal(const void *src, const UA_DataType *type, const UA_String *namespaces, size_t namespaceSize, const UA_String *serverUris, size_t serverUriSize, UA_Boolean useReversible)
Returns the number of bytes the value src takes in json encoding.
UA_StatusCode writeJsonKey(CtxJson *ctx, const char *key)
UA_StatusCode writeJsonObjStart(CtxJson *ctx)
UA_StatusCode writeJsonObjEnd(CtxJson *ctx)
UA_StatusCode tokenize(ParseCtx *parseCtx, CtxJson *ctx, const UA_ByteString *src, size_t tokensSize)
UA_StatusCode writeJsonArrElm(CtxJson *ctx, const void *value, const UA_DataType *type)
UA_StatusCode lookAheadForKey(const char *search, CtxJson *ctx, ParseCtx *parseCtx, size_t *resultIndex)
UA_StatusCode(* decodeJsonSignature)(void *dst, const UA_DataType *type, CtxJson *ctx, ParseCtx *parseCtx)
UA_StatusCode decodeFields(CtxJson *ctx, ParseCtx *parseCtx, DecodeEntry *entries, size_t entryCount)
UA_StatusCode writeJsonCommaIfNeeded(CtxJson *ctx)
UA_StatusCode status