16static void emit_uint(
JsonWriter &w, uint64_t v)
29 rev[r++] = (char)(
'0' + (
int)(v % 10));
46static void emit_args(
JsonWriter &w,
const char *args_json,
const char *kwargs_json)
48 if (!args_json && !kwargs_json)
52 w.
raw(args_json ? args_json :
"[]");
59size_t pc_wamp_build_hello(
char *buf,
size_t cap,
const char *realm,
const char *details_json)
69 w.
raw(details_json ? details_json :
"{}");
74size_t pc_wamp_build_goodbye(
char *buf,
size_t cap,
const char *reason_uri,
const char *details_json)
76 if (!buf || !reason_uri)
83 w.
raw(details_json ? details_json :
"{}");
89size_t pc_wamp_build_subscribe(
char *buf,
size_t cap, uint64_t request,
const char *topic,
const char *options_json)
98 emit_uint(w, request);
99 w.
raw(options_json ? options_json :
"{}");
105size_t pc_wamp_build_unsubscribe(
char *buf,
size_t cap, uint64_t request, uint64_t subscription_id)
114 emit_uint(w, request);
115 emit_uint(w, subscription_id);
120size_t pc_wamp_build_unregister(
char *buf,
size_t cap, uint64_t request, uint64_t registration_id)
129 emit_uint(w, request);
130 emit_uint(w, registration_id);
135size_t pc_wamp_build_publish(
char *buf,
size_t cap, uint64_t request,
const char *topic,
const char *options_json,
136 const char *args_json,
const char *kwargs_json)
145 emit_uint(w, request);
146 w.
raw(options_json ? options_json :
"{}");
148 emit_args(w, args_json, kwargs_json);
153size_t pc_wamp_build_call(
char *buf,
size_t cap, uint64_t request,
const char *procedure,
const char *options_json,
154 const char *args_json,
const char *kwargs_json)
156 if (!buf || !procedure)
163 emit_uint(w, request);
164 w.
raw(options_json ? options_json :
"{}");
166 emit_args(w, args_json, kwargs_json);
171size_t pc_wamp_build_register(
char *buf,
size_t cap, uint64_t request,
const char *procedure,
const char *options_json)
173 if (!buf || !procedure)
180 emit_uint(w, request);
181 w.
raw(options_json ? options_json :
"{}");
187size_t pc_wamp_build_yield(
char *buf,
size_t cap, uint64_t request,
const char *options_json,
const char *args_json,
188 const char *kwargs_json)
197 emit_uint(w, request);
198 w.
raw(options_json ? options_json :
"{}");
199 emit_args(w, args_json, kwargs_json);
206static size_t skip_ws(
const char *s,
size_t i)
208 while (s[i] ==
' ' || s[i] ==
'\t' || s[i] ==
'\n' || s[i] ==
'\r')
216static size_t scan_string(
const char *s,
size_t i)
240static size_t scan_value(
const char *s,
size_t i)
244 return scan_string(s, i);
246 if (s[i] ==
'{' || s[i] ==
'[')
248 char open = s[i], close = (open ==
'{') ?
'}' :
']';
254 size_t e = scan_string(s, i);
266 else if (s[i] == close)
280 while (s[i] && s[i] !=
',' && s[i] !=
']' && s[i] !=
'}' && s[i] !=
' ' && s[i] !=
'\t' && s[i] !=
'\n' &&
285 return i > start ? i : 0;
288bool pc_wamp_element(
const char *msg,
size_t index,
const char **start,
size_t *len)
294 size_t i = skip_ws(msg, 0);
300 for (
size_t idx = 0;; idx++)
303 if (msg[i] ==
']' || msg[i] ==
'\0')
308 size_t e = scan_value(msg, i);
337bool pc_wamp_get_uint(
const char *msg,
size_t index, uint64_t *out)
343 if (!pc_wamp_element(msg, index, &s, &n) || n == 0)
348 for (
size_t i = 0; i < n; i++)
350 if (s[i] <
'0' || s[i] >
'9')
354 v = v * 10 + (uint64_t)(s[i] -
'0');
363bool pc_wamp_get_type(
const char *msg,
int *out)
366 if (!pc_wamp_get_uint(msg, 0, &v))
377bool pc_wamp_get_uri(
const char *msg,
size_t index,
char *out,
size_t out_cap)
381 if (!out || out_cap == 0 || !pc_wamp_element(msg, index, &s, &n))
388 if (n < 2 || s[0] !=
'"' || s[n - 1] !=
'"')
393 if (body + 1 > out_cap)
397 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 (PC_ENABLE_WAMP) - zero-heap builders + a positional ...