void PolicyPrintAsJson(Writer *writer, const char *filename, Bundle *bundles, Body *bodies) { JsonElement *json_policy = JsonObjectCreate(10); JsonObjectAppendString(json_policy, "name", filename); { JsonElement *json_bundles = JsonArrayCreate(10); Bundle *bp = NULL; for (bp = bundles; bp != NULL; bp = bp->next) { JsonArrayAppendObject(json_bundles, ExportBundleAsJson(bp)); } JsonObjectAppendArray(json_policy, "bundles", json_bundles); } { JsonElement *json_bodies = JsonArrayCreate(10); Body *bdp = NULL; for (bdp = bodies; bdp != NULL; bdp = bdp->next) { JsonArrayAppendObject(json_bodies, ExportBodyAsJson(bdp)); } JsonObjectAppendArray(json_policy, "bodies", json_bodies); } JsonElementPrint(writer, json_policy, 0); JsonElementDestroy(json_policy); }
static JsonElement *ExportAttributeValueAsJson(Rval rval) { JsonElement *json_attribute = JsonObjectCreate(10); switch (rval.rtype) { case CF_SCALAR: { char buffer[CF_BUFSIZE]; EscapeQuotes((const char *) rval.item, buffer, sizeof(buffer)); JsonObjectAppendString(json_attribute, "type", "string"); JsonObjectAppendString(json_attribute, "value", buffer); } return json_attribute; case CF_LIST: { Rlist *rp = NULL; JsonElement *list = JsonArrayCreate(10); JsonObjectAppendString(json_attribute, "type", "list"); for (rp = (Rlist *) rval.item; rp != NULL; rp = rp->next) { JsonArrayAppendObject(list, ExportAttributeValueAsJson((Rval) {rp->item, rp->type})); } JsonObjectAppendArray(json_attribute, "value", list); return json_attribute; } case CF_FNCALL: { Rlist *argp = NULL; FnCall *call = (FnCall *) rval.item; JsonObjectAppendString(json_attribute, "type", "function-call"); JsonObjectAppendString(json_attribute, "name", call->name); { JsonElement *arguments = JsonArrayCreate(10); for (argp = call->args; argp != NULL; argp = argp->next) { JsonArrayAppendObject(arguments, ExportAttributeValueAsJson((Rval) {argp->item, argp->type})); } JsonObjectAppendArray(json_attribute, "arguments", arguments); } return json_attribute; } default: FatalError("Attempted to export attribute of type: %c", rval.rtype); return NULL; } }
static JsonElement *ExportBundleClassesAsJson(Promise *promises) { JsonElement *json_contexts = JsonArrayCreate(10); JsonElement *json_promises = JsonArrayCreate(10); char *current_context = "any"; size_t context_offset_start = -1; size_t context_offset_end = -1; Promise *pp = NULL; for (pp = promises; pp != NULL; pp = pp->next) { JsonElement *json_promise = JsonObjectCreate(10); JsonObjectAppendInteger(json_promise, "offset", pp->offset.start); { JsonElement *json_promise_attributes = JsonArrayCreate(10); Constraint *cp = NULL; for (cp = pp->conlist; cp != NULL; cp = cp->next) { JsonElement *json_attribute = JsonObjectCreate(10); JsonObjectAppendInteger(json_attribute, "offset", cp->offset.start); JsonObjectAppendInteger(json_attribute, "offset-end", cp->offset.end); context_offset_end = cp->offset.end; JsonObjectAppendString(json_attribute, "lval", cp->lval); JsonObjectAppendObject(json_attribute, "rval", ExportAttributeValueAsJson(cp->rval)); JsonArrayAppendObject(json_promise_attributes, json_attribute); } JsonObjectAppendInteger(json_promise, "offset-end", context_offset_end); JsonObjectAppendString(json_promise, "promiser", pp->promiser); /* FIXME: does not work for lists */ if (pp->promisee.rtype == CF_SCALAR || pp->promisee.rtype == CF_NOPROMISEE) { JsonObjectAppendString(json_promise, "promisee", pp->promisee.item); } JsonObjectAppendArray(json_promise, "attributes", json_promise_attributes); } JsonArrayAppendObject(json_promises, json_promise); if (pp->next == NULL || strcmp(current_context, pp->next->classes) != 0) { JsonArrayAppendObject(json_contexts, CreateContextAsJson(current_context, context_offset_start, context_offset_end, "promises", json_promises)); current_context = pp->classes; } } return json_contexts; }
static JsonElement *RlistToJson(Rlist *list) { JsonElement *array = JsonArrayCreate(RlistLen(list)); for (Rlist *rp = list; rp; rp = rp->next) { switch (rp->val.type) { case RVAL_TYPE_SCALAR: JsonArrayAppendString(array, RlistScalarValue(rp)); break; case RVAL_TYPE_LIST: JsonArrayAppendArray(array, RlistToJson(RlistRlistValue(rp))); break; case RVAL_TYPE_FNCALL: JsonArrayAppendObject(array, FnCallToJson(RlistFnCallValue(rp))); break; default: assert(false && "Unsupported item type in rlist"); break; } } return array; }
static JsonElement *FnCallToJson(const FnCall *fp) { assert(fp); JsonElement *object = JsonObjectCreate(3); JsonObjectAppendString(object, "name", fp->name); JsonObjectAppendString(object, "type", "function-call"); JsonElement *argsArray = JsonArrayCreate(5); for (Rlist *rp = fp->args; rp != NULL; rp = rp->next) { switch (rp->val.type) { case RVAL_TYPE_SCALAR: JsonArrayAppendString(argsArray, RlistScalarValue(rp)); break; case RVAL_TYPE_FNCALL: JsonArrayAppendObject(argsArray, FnCallToJson(RlistFnCallValue(rp))); break; default: assert(false && "Unknown argument type"); break; } } JsonObjectAppendArray(object, "arguments", argsArray); return object; }
JsonElement *FnCallToJson(FnCall *fp) { assert(fp); JsonElement *object = JsonObjectCreate(3); JsonObjectAppendString(object, "name", fp->name); JsonObjectAppendString(object, "type", "function-call"); JsonElement *argsArray = JsonArrayCreate(fp->argc); for (Rlist *rp = fp->args; rp != NULL; rp = rp->next) { switch (rp->type) { case CF_SCALAR: JsonArrayAppendString(argsArray, (const char *) rp->item); break; case CF_FNCALL: JsonArrayAppendObject(argsArray, FnCallToJson((FnCall *) rp->item)); break; default: assert(false && "Unknown argument type"); break; } } JsonObjectAppendArray(object, "arguments", argsArray); return object; }
static JsonElement *FnCallTypeToJson(const FnCallType *fn_syntax) { JsonElement *json_fn = JsonObjectCreate(10); JsonObjectAppendString(json_fn, "status", SyntaxStatusToString(fn_syntax->status)); JsonObjectAppendString(json_fn, "returnType", DataTypeToString(fn_syntax->dtype)); { JsonElement *params = JsonArrayCreate(10); for (int i = 0; fn_syntax->args[i].pattern; i++) { const FnCallArg *param = &fn_syntax->args[i]; JsonElement *json_param = JsonObjectCreate(2); JsonObjectAppendString(json_param, "type", DataTypeToString(param->dtype)); JsonObjectAppendString(json_param, "range", param->pattern); JsonArrayAppendObject(params, json_param); } JsonObjectAppendArray(json_fn, "parameters", params); } JsonObjectAppendBool(json_fn, "variadic", fn_syntax->options & FNCALL_OPTION_VARARG); JsonObjectAppendBool(json_fn, "cached", fn_syntax->options & FNCALL_OPTION_CACHED); JsonObjectAppendString(json_fn, "category", FnCallCategoryToString(fn_syntax->category)); return json_fn; }
static JsonElement *ExportBodyClassesAsJson(Constraint *constraints) { JsonElement *json_contexts = JsonArrayCreate(10); JsonElement *json_attributes = JsonArrayCreate(10); char *current_context = "any"; size_t context_offset_start = -1; size_t context_offset_end = -1; Constraint *cp = NULL; for (cp = constraints; cp != NULL; cp = cp->next) { JsonElement *json_attribute = JsonObjectCreate(10); JsonObjectAppendInteger(json_attribute, "offset", cp->offset.start); JsonObjectAppendInteger(json_attribute, "offset-end", cp->offset.end); context_offset_start = cp->offset.context; context_offset_end = cp->offset.end; JsonObjectAppendString(json_attribute, "lval", cp->lval); JsonObjectAppendObject(json_attribute, "rval", ExportAttributeValueAsJson(cp->rval)); JsonArrayAppendObject(json_attributes, json_attribute); if (cp->next == NULL || strcmp(current_context, cp->next->classes) != 0) { JsonArrayAppendObject(json_contexts, CreateContextAsJson(current_context, context_offset_start, context_offset_end, "attributes", json_attributes)); current_context = cp->classes; } } return json_contexts; }
static void test_show_array_object(void **state) { JsonElement *array = JsonArrayCreate(10); JsonElement *object = JsonObjectCreate(10); JsonObjectAppendString(object, "first", "one"); JsonArrayAppendObject(array, object); Writer *writer = StringWriter(); JsonElementPrint(writer, array, 0); assert_string_equal(ARRAY_OBJECT, StringWriterData(writer)); JsonElementDestroy(array); }
static void test_show_array_object(void) { JsonElement *array = JsonArrayCreate(10); JsonElement *object = JsonObjectCreate(10); JsonObjectAppendString(object, "first", "one"); JsonArrayAppendObject(array, object); Writer *writer = StringWriter(); JsonWrite(writer, array, 0); char *output = StringWriterClose(writer); assert_string_equal(ARRAY_OBJECT, output); JsonDestroy(array); free(output); }
static JsonElement *ExportBundleAsJson(Bundle *bundle) { JsonElement *json_bundle = JsonObjectCreate(10); JsonObjectAppendInteger(json_bundle, "offset", bundle->offset.start); JsonObjectAppendInteger(json_bundle, "offset-end", bundle->offset.end); JsonObjectAppendString(json_bundle, "name", bundle->name); JsonObjectAppendString(json_bundle, "bundle-type", bundle->type); { JsonElement *json_args = JsonArrayCreate(10); Rlist *argp = NULL; for (argp = bundle->args; argp != NULL; argp = argp->next) { JsonArrayAppendString(json_args, argp->item); } JsonObjectAppendArray(json_bundle, "arguments", json_args); } { JsonElement *json_promise_types = JsonArrayCreate(10); SubType *sp = NULL; for (sp = bundle->subtypes; sp != NULL; sp = sp->next) { JsonElement *json_promise_type = JsonObjectCreate(10); JsonObjectAppendInteger(json_promise_type, "offset", sp->offset.start); JsonObjectAppendInteger(json_promise_type, "offset-end", sp->offset.end); JsonObjectAppendString(json_promise_type, "name", sp->name); JsonObjectAppendArray(json_promise_type, "classes", ExportBundleClassesAsJson(sp->promiselist)); JsonArrayAppendObject(json_promise_types, json_promise_type); } JsonObjectAppendArray(json_bundle, "promise-types", json_promise_types); } return json_bundle; }