ua_types_encoding_json.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. #ifndef UA_TYPES_ENCODING_JSON_H_
  9. #define UA_TYPES_ENCODING_JSON_H_
  10. #include <open62541/types.h>
  11. #include "ua_types_encoding_binary.h"
  12. #include "ua_types_encoding_json.h"
  13. #include "ua_util_internal.h"
  14. #include "../deps/jsmn/jsmn.h"
  15. _UA_BEGIN_DECLS
  16. #define UA_JSON_MAXTOKENCOUNT 1000
  17. size_t
  18. UA_calcSizeJson(const void *src, const UA_DataType *type,
  19. UA_String *namespaces, size_t namespaceSize,
  20. UA_String *serverUris, size_t serverUriSize,
  21. UA_Boolean useReversible) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  22. UA_StatusCode
  23. UA_encodeJson(const void *src, const UA_DataType *type,
  24. uint8_t **bufPos, const uint8_t **bufEnd,
  25. UA_String *namespaces, size_t namespaceSize,
  26. UA_String *serverUris, size_t serverUriSize,
  27. UA_Boolean useReversible) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  28. UA_StatusCode
  29. UA_decodeJson(const UA_ByteString *src, void *dst,
  30. const UA_DataType *type) UA_FUNC_ATTR_WARN_UNUSED_RESULT;
  31. /* Interal Definitions
  32. *
  33. * For future by the PubSub encoding */
  34. #define UA_JSON_ENCODING_MAX_RECURSION 100
  35. typedef struct {
  36. uint8_t *pos;
  37. const uint8_t *end;
  38. uint16_t depth; /* How often did we en-/decoding recurse? */
  39. UA_Boolean commaNeeded[UA_JSON_ENCODING_MAX_RECURSION];
  40. UA_Boolean useReversible;
  41. UA_Boolean calcOnly; /* Only compute the length of the decoding */
  42. size_t namespacesSize;
  43. UA_String *namespaces;
  44. size_t serverUrisSize;
  45. UA_String *serverUris;
  46. } CtxJson;
  47. UA_StatusCode writeJsonObjStart(CtxJson *ctx);
  48. UA_StatusCode writeJsonObjElm(CtxJson *ctx, const char *key,
  49. const void *value, const UA_DataType *type);
  50. UA_StatusCode writeJsonObjEnd(CtxJson *ctx);
  51. UA_StatusCode writeJsonArrStart(CtxJson *ctx);
  52. UA_StatusCode writeJsonArrElm(CtxJson *ctx, const void *value,
  53. const UA_DataType *type);
  54. UA_StatusCode writeJsonArrEnd(CtxJson *ctx);
  55. UA_StatusCode writeJsonKey(CtxJson *ctx, const char* key);
  56. UA_StatusCode writeJsonCommaIfNeeded(CtxJson *ctx);
  57. UA_StatusCode writeJsonNull(CtxJson *ctx);
  58. /* The encoding length is returned in ctx->pos */
  59. static UA_INLINE UA_StatusCode
  60. calcJsonObjStart(CtxJson *ctx) {
  61. UA_assert(ctx->calcOnly);
  62. return writeJsonObjStart(ctx);
  63. }
  64. static UA_INLINE UA_StatusCode
  65. calcJsonObjElm(CtxJson *ctx, const char *key,
  66. const void *value, const UA_DataType *type) {
  67. UA_assert(ctx->calcOnly);
  68. return writeJsonObjElm(ctx, key, value, type);
  69. }
  70. static UA_INLINE UA_StatusCode
  71. calcJsonObjEnd(CtxJson *ctx) {
  72. UA_assert(ctx->calcOnly);
  73. return writeJsonObjEnd(ctx);
  74. }
  75. static UA_INLINE UA_StatusCode
  76. calcJsonArrStart(CtxJson *ctx) {
  77. UA_assert(ctx->calcOnly);
  78. return writeJsonArrStart(ctx);
  79. }
  80. static UA_INLINE UA_StatusCode
  81. calcJsonArrElm(CtxJson *ctx, const void *value,
  82. const UA_DataType *type) {
  83. UA_assert(ctx->calcOnly);
  84. return writeJsonArrElm(ctx, value, type);
  85. }
  86. static UA_INLINE UA_StatusCode
  87. calcJsonArrEnd(CtxJson *ctx) {
  88. UA_assert(ctx->calcOnly);
  89. return writeJsonArrEnd(ctx);
  90. }
  91. status
  92. encodeJsonInternal(const void *src, const UA_DataType *type, CtxJson *ctx);
  93. typedef struct {
  94. jsmntok_t *tokenArray;
  95. UA_Int32 tokenCount;
  96. UA_UInt16 index;
  97. /* Additonal data for special cases such as networkmessage/datasetmessage
  98. * Currently only used for dataSetWriterIds */
  99. size_t numCustom;
  100. void * custom;
  101. size_t* currentCustomIndex;
  102. } ParseCtx;
  103. typedef UA_StatusCode
  104. (*encodeJsonSignature)(const void *src, const UA_DataType *type, CtxJson *ctx);
  105. typedef UA_StatusCode
  106. (*decodeJsonSignature)(void *dst, const UA_DataType *type, CtxJson *ctx,
  107. ParseCtx *parseCtx, UA_Boolean moveToken);
  108. /* Map for decoding a Json Object. An array of this is passed to the
  109. * decodeFields function. If the key "fieldName" is found in the json object
  110. * (mark as found and) decode the value with the "function" and write result
  111. * into "fieldPointer" (destination). */
  112. typedef struct {
  113. const char * fieldName;
  114. void * fieldPointer;
  115. decodeJsonSignature function;
  116. UA_Boolean found;
  117. const UA_DataType *type;
  118. } DecodeEntry;
  119. UA_StatusCode
  120. decodeFields(CtxJson *ctx, ParseCtx *parseCtx,
  121. DecodeEntry *entries, size_t entryCount,
  122. const UA_DataType *type);
  123. UA_StatusCode
  124. decodeJsonInternal(void *dst, const UA_DataType *type,
  125. CtxJson *ctx, ParseCtx *parseCtx, UA_Boolean moveToken);
  126. /* workaround: TODO generate functions for UA_xxx_decodeJson */
  127. decodeJsonSignature getDecodeSignature(u8 index);
  128. UA_StatusCode lookAheadForKey(const char* search, CtxJson *ctx, ParseCtx *parseCtx, size_t *resultIndex);
  129. jsmntype_t getJsmnType(const ParseCtx *parseCtx);
  130. UA_StatusCode tokenize(ParseCtx *parseCtx, CtxJson *ctx, const UA_ByteString *src);
  131. UA_Boolean isJsonNull(const CtxJson *ctx, const ParseCtx *parseCtx);
  132. _UA_END_DECLS
  133. #endif /* UA_TYPES_ENCODING_JSON_H_ */