Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_json.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * AFW JSON Miscellaneous Functions
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 /* Declares and rti/inf defines for interface afw_content_type */
19 #define AFW_IMPLEMENTATION_ID AFW_JSON_Q_CONTENT_TYPE_SHORT
21 
22 
23 /* Media types supported by this afw_content_type implementation. */
24 static const afw_utf8_t impl_media_types[] = {
25  AFW_UTF8_LITERAL(AFW_JSON_Q_CONTENT_TYPE),
26  AFW_UTF8_LITERAL("*") /* Let JSON handle *. */
27 };
28 
29 /* Raw begin object list. */
30 static const afw_memory_t impl_raw_begin_object_list = {
31  (const afw_octet_t *)"[\n",
32  sizeof("[\n")-1
33 };
34 
35 /* Raw object separator. */
36 static const afw_memory_t impl_raw_object_separator = {
37  (const afw_octet_t *)",\n",
38  sizeof(",\n") - 1
39 };
40 
41 /* Raw last object separator. */
42 static const afw_memory_t impl_raw_last_object_separator = {
43  (const afw_octet_t *)"\n",
44  sizeof("\n") - 1
45 };
46 
47 /* Raw end object list. */
48 static const afw_memory_t impl_raw_end_object_list = {
49  (const afw_octet_t *)"]\n",
50  sizeof("]\n") - 1
51 };
52 
53 /* Content type singleton instance for this implementation. */
54 static const afw_content_type_t impl_afw_content_type =
55 {
56  &impl_afw_content_type_inf,
58  sizeof(impl_media_types) / sizeof(afw_utf8_t),
59  &impl_media_types[0]
60 };
61 
62 
63 /* Function to return the content type singleton. */
66 {
67  return &impl_afw_content_type;
68 }
69 
70 
71 /* Register json support. */
73 {
75 }
76 
77 
78 /*
79  * Implementation of method to_value of interface afw_content_type.
80  */
81 const afw_value_t *
82 impl_afw_content_type_raw_to_value(
83  const afw_content_type_t * instance,
84  const afw_memory_t * raw,
85  const afw_utf8_t * source_location,
86  const afw_pool_t * p,
87  afw_xctx_t *xctx)
88 {
89  return afw_json_to_value(afw_utf8_from_raw(raw, p, xctx),
90  source_location, p, xctx);
91 }
92 
93 
94 
95 /*
96  * Implementation of method raw_to_object of interface afw_content_type.
97  */
98 const afw_object_t *
99 impl_afw_content_type_raw_to_object(
100  const afw_content_type_t * instance,
101  const afw_memory_t * raw,
102  const afw_utf8_t * source_location,
103  const afw_utf8_t * adaptor_id,
104  const afw_utf8_t * object_type_id,
105  const afw_utf8_t * object_id,
106  afw_boolean_t cede_p,
107  const afw_pool_t * p,
108  afw_xctx_t *xctx)
109 {
110  const afw_pool_t *use_p;
111  const afw_utf8_t *string;
112 
113  /* If not cede_p, get subpool now to use for afw_utf8_from_raw(). */
114  use_p = p;
115  if (!cede_p) {
116  use_p = afw_pool_create(p, xctx);
117  }
118 
119  /* Convert raw to utf8. An error may be thrown if not valid utf-8. */
120  string = afw_utf8_from_raw(raw, use_p, xctx);
121 
122  /*
123  * Call afw_compile_to_object() to convert json to object. Always cede_p
124  * since subpool has already been created. Return result.
125  */
126  return afw_json_to_object_with_meta(string, source_location,
127  adaptor_id, object_type_id, object_id, true, p, xctx);
128 }
129 
130 
131 
132 /*
133  * Implementation of method write_value of interface afw_content_type.
134  */
135 void
136 impl_afw_content_type_write_value(
137  const afw_content_type_t * instance,
138  const afw_value_t * value,
139  const afw_object_options_t *options,
140  void * context,
141  afw_write_cb_t callback,
142  const afw_pool_t *p, afw_xctx_t *xctx)
143 {
144  afw_json_internal_write_value(value, options, context, callback,
145  p, xctx);
146 }
147 
148 
149 
150 /*
151  * Implementation of method create_object_list_writer of interface afw_content_type.
152  */
154 impl_afw_content_type_create_object_list_writer(
155  const afw_content_type_t * instance,
156  const afw_object_options_t *options,
157  void * context,
158  afw_write_cb_t callback,
159  const afw_pool_t *p, afw_xctx_t *xctx)
160 {
161  return afw_content_type_impl_create_object_list_writer(
162  instance, options, context, callback,
163  &impl_raw_begin_object_list,
164  &impl_raw_object_separator,
165  &impl_raw_last_object_separator,
166  &impl_raw_end_object_list,
167  p, xctx);
168 }
AFW_DEFINE(const afw_object_t *)
Interface afw_interface implementation declares.
Adaptive Framework Core Internal.
#define AFW_IMPLEMENTATION_ID
#define AFW_UTF8_LITERAL(A_STRING)
String literal initializer.
Definition: afw_common.h:582
_Bool afw_boolean_t
Definition: afw_common.h:373
unsigned char afw_octet_t
8 bits (unsigned).
Definition: afw_common.h:211
afw_size_t(* afw_write_cb_t)(void *context, const void *buffer, afw_size_t size, const afw_pool_t *p, afw_xctx_t *xctx)
Typedef for write callback function.
Definition: afw_common.h:1226
afw_content_type_register(const afw_content_type_t *content_type, afw_xctx_t *xctx)
Register content type with its id and all of its media types.
#define afw_json_to_value(json, source_location, p, xctx)
Convert JSON to an adaptive value.
Definition: afw_json.h:128
void afw_json_register(afw_xctx_t *xctx)
Register JSON support.
#define afw_json_to_object_with_meta(string, source_location, adaptor_id, object_type_id, object_id, cede_p, p, xctx)
Compile json string to adaptive object with meta.
Definition: afw_json.h:167
afw_json_content_type_get()
Get the content type instance for json.
Definition: afw_json.c:65
const afw_pool_t * afw_pool_create(const afw_pool_t *parent, afw_xctx_t *xctx)
Create a new pool.
const afw_utf8_t * afw_utf8_from_raw(const afw_memory_t *raw, const afw_pool_t *p, afw_xctx_t *xctx)
Convert raw to a utf-8 NFC normalizing if necessary in specified pool.
Definition: afw_utf8.h:199
Interface afw_content_type_object_list_writer public struct.
Interface afw_content_type public struct.
Struct for memory pointer and size.
Definition: afw_common.h:505
Struct for object processing options.
Interface afw_object public struct.
Interface afw_pool public struct.
NFC normalized UTF-8 string.
Definition: afw_common.h:545
Interface afw_value public struct.
Interface afw_xctx public struct.