MCPcopy Create free account
hub / github.com/MariaDB/server / Serialize

Function Serialize

storage/connect/json.cpp:258–340  ·  view source on GitHub ↗

/ Serialize a JSON document tree: */ /

Source from the content-addressed store, hash-verified

256/* Serialize a JSON document tree: */
257/***********************************************************************/
258PSZ Serialize(PGLOBAL g, PJSON jsp, char* fn, int pretty) {
259 PSZ str = NULL;
260 bool b = false, err = true;
261 JOUT* jp;
262 FILE* fs = NULL;
263 PJDOC jdp = NULL;
264
265 g->Message[0] = 0;
266
267 try {
268 jdp = new(g) JDOC; // MUST BE ALLOCATED BEFORE jp !!!!!
269 jdp->dfp = GetDefaultPrec();
270
271 if (!jsp) {
272 snprintf(g->Message, sizeof(g->Message), "Null json tree");
273 throw 1;
274 } else if (!fn) {
275 // Serialize to a string
276 jp = new(g) JOUTSTR(g);
277 b = pretty == 1;
278 } else {
279 if (!(fs = fopen(fn, "wb"))) {
280 snprintf(g->Message, sizeof(g->Message), MSG(OPEN_MODE_ERROR) ": %s",
281 "w", (int)errno, fn, strerror(errno));
282 throw 2;
283 } else if (pretty >= 2) {
284 // Serialize to a pretty file
285 jp = new(g)JOUTPRT(g, fs);
286 } else {
287 // Serialize to a flat file
288 b = true;
289 jp = new(g)JOUTFILE(g, fs, pretty);
290 } // endif's
291
292 } // endif's
293
294 jdp->SetJp(jp);
295
296 switch (jsp->GetType()) {
297 case TYPE_JAR:
298 err = jdp->SerializeArray((PJAR)jsp, b);
299 break;
300 case TYPE_JOB:
301 err = ((b && jp->Prty()) && jp->WriteChr('\t'));
302 err |= jdp->SerializeObject((PJOB)jsp);
303 break;
304 case TYPE_JVAL:
305 err = jdp->SerializeValue((PJVAL)jsp);
306 break;
307 default:
308 snprintf(g->Message, sizeof(g->Message), "Invalid json tree");
309 } // endswitch Type
310
311 if (fs) {
312 fputs(EL, fs);
313 fclose(fs);
314 if(err) {
315 str = NULL;

Callers 15

SetJsonValueMethod · 0.85
MakeResultMethod · 0.85
PrepareWritingMethod · 0.85
MakeJsonMethod · 0.85
CloseDBMethod · 0.85
SetJsonValueMethod · 0.85
MakeResultFunction · 0.85
jsonvalueFunction · 0.85
json_make_arrayFunction · 0.85
json_make_objectFunction · 0.85
json_object_nonullFunction · 0.85
json_object_keyFunction · 0.85

Calls 10

GetDefaultPrecFunction · 0.85
PlugSubAllocFunction · 0.85
SetJpMethod · 0.80
PrtyMethod · 0.80
WriteChrMethod · 0.80
htrcFunction · 0.70
GetTypeMethod · 0.45
SerializeArrayMethod · 0.45
SerializeObjectMethod · 0.45
SerializeValueMethod · 0.45

Tested by

no test coverage detected