16static void emit_uint(
JsonWriter &w, uint64_t v)
29 rev[r++] = (char)(
'0' + (
int)(v % 10));
44static void emit_args(
JsonWriter &w,
const char *args_json,
const char *kwargs_json)
46 if (!args_json && !kwargs_json)
48 w.
raw(args_json ? args_json :
"[]");
53size_t wamp_build_hello(
char *buf,
size_t cap,
const char *realm,
const char *details_json)
61 w.
raw(details_json ? details_json :
"{}");
66size_t wamp_build_goodbye(
char *buf,
size_t cap,
const char *reason_uri,
const char *details_json)
68 if (!buf || !reason_uri)
73 w.
raw(details_json ? details_json :
"{}");
79size_t wamp_build_subscribe(
char *buf,
size_t cap, uint64_t request,
const char *topic,
const char *options_json)
86 emit_uint(w, request);
87 w.
raw(options_json ? options_json :
"{}");
93size_t wamp_build_unsubscribe(
char *buf,
size_t cap, uint64_t request, uint64_t subscription_id)
100 emit_uint(w, request);
101 emit_uint(w, subscription_id);
106size_t wamp_build_publish(
char *buf,
size_t cap, uint64_t request,
const char *topic,
const char *options_json,
107 const char *args_json,
const char *kwargs_json)
114 emit_uint(w, request);
115 w.
raw(options_json ? options_json :
"{}");
117 emit_args(w, args_json, kwargs_json);
122size_t wamp_build_call(
char *buf,
size_t cap, uint64_t request,
const char *procedure,
const char *options_json,
123 const char *args_json,
const char *kwargs_json)
125 if (!buf || !procedure)
130 emit_uint(w, request);
131 w.
raw(options_json ? options_json :
"{}");
133 emit_args(w, args_json, kwargs_json);
138size_t wamp_build_register(
char *buf,
size_t cap, uint64_t request,
const char *procedure,
const char *options_json)
140 if (!buf || !procedure)
145 emit_uint(w, request);
146 w.
raw(options_json ? options_json :
"{}");
152size_t wamp_build_yield(
char *buf,
size_t cap, uint64_t request,
const char *options_json,
const char *args_json,
153 const char *kwargs_json)
160 emit_uint(w, request);
161 w.
raw(options_json ? options_json :
"{}");
162 emit_args(w, args_json, kwargs_json);
169static size_t skip_ws(
const char *s,
size_t i)
171 while (s[i] ==
' ' || s[i] ==
'\t' || s[i] ==
'\n' || s[i] ==
'\r')
177static size_t scan_string(
const char *s,
size_t i)
197static size_t scan_value(
const char *s,
size_t i)
200 return scan_string(s, i);
201 if (s[i] ==
'{' || s[i] ==
'[')
203 char open = s[i], close = (open ==
'{') ?
'}' :
']';
209 size_t e = scan_string(s, i);
217 else if (s[i] == close)
229 while (s[i] && s[i] !=
',' && s[i] !=
']' && s[i] !=
'}' && s[i] !=
' ' && s[i] !=
'\t' && s[i] !=
'\n' &&
232 return i > start ? i : 0;
235bool wamp_element(
const char *msg,
size_t index,
const char **start,
size_t *len)
239 size_t i = skip_ws(msg, 0);
243 for (
size_t idx = 0;; idx++)
246 if (msg[i] ==
']' || msg[i] ==
'\0')
249 size_t e = scan_value(msg, i);
268bool wamp_get_uint(
const char *msg,
size_t index, uint64_t *out)
272 if (!wamp_element(msg, index, &s, &n) || n == 0)
275 for (
size_t i = 0; i < n; i++)
277 if (s[i] <
'0' || s[i] >
'9')
279 v = v * 10 + (uint64_t)(s[i] -
'0');
286bool wamp_get_type(
const char *msg,
int *out)
289 if (!wamp_get_uint(msg, 0, &v))
296bool wamp_get_uri(
const char *msg,
size_t index,
char *out,
size_t out_cap)
300 if (!out || out_cap == 0 || !wamp_element(msg, index, &s, &n))
302 if (n < 2 || s[0] !=
'"' || s[n - 1] !=
'"')
305 if (body + 1 > out_cap)
307 for (
size_t i = 0; i < body; i++)
Builds a JSON document into a fixed caller buffer, no heap.
void str(const char *v)
Emit a quoted, escaped string value.
void raw(const char *literal)
Emit a pre-formatted literal verbatim.
void integer(long v)
Emit a signed integer value.
bool ok() const
< False after any overflow / structural error.
size_t length() const
< Bytes written so far (excludes the NUL).
void begin_array()
Open [.
Layer 6 (Presentation) - zero-heap JSON: a bounded writer and top-level reader.
WAMP (Web Application Messaging Protocol) codec (DETWS_ENABLE_WAMP) - zero-heap builders + a position...