ua_types_encoding_json.h 5.2 KB

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