Просмотр исходного кода

Add JSON serialization of any model

Yentl Van Tendeloo 8 лет назад
Родитель
Сommit
80f3c44e87
1 измененных файлов с 38 добавлено и 1 удалено
  1. 38 1
      core/core_algorithm.alc

+ 38 - 1
core/core_algorithm.alc

@@ -15,6 +15,43 @@ Element core = ?
 String core_location = "models/CoreFormalism"
 String core_model_location = "core"
 
+String function JSON_print(model : Element):
+	String result
+	Element keys_m
+	String v_m
+	String type
+	String attr_key
+	Element attr_keys
+
+	result = "["
+	keys_m = dict_keys(model["model"])
+
+	while (read_nr_out(keys_m) > 0):
+		v_m = set_pop(keys_m)
+		type = read_type(model["metamodel"], read_type(model, v_m))
+
+		if (bool_or(type == "Class", type == "Association")):
+			result = result + "{"
+			result = (((result + "\"id\": \"") + v_m) + "\"") + ","
+			result = (((result + "\"type\": \"") + read_type(model, v_m)) + "\"") + ","
+			if (type == "Association"):
+				result = ((result + "\"__source\": \"") + reverseKeyLookup(model["model"], read_edge_src(model["model"][v_m]))) + "\","
+				result = ((result + "\"__target\": \"") + reverseKeyLookup(model["model"], read_edge_dst(model["model"][v_m]))) + "\","
+
+			// Has attributes
+			attr_keys = dict_keys(getAttributeList(model, v_m))
+			while (0 < read_nr_out(attr_keys)):
+				attr_key = set_pop(attr_keys)
+				result = ((((result + "\"") + attr_key) + "\": ") + cast_v2s(read_attribute(model, v_m, attr_key))) + ","
+
+			result = result + "}"
+
+			if (read_nr_out(keys_m) > 0):
+				result = result + ","
+	result = result + "]"
+
+	return result!
+
 Void function main():
 	// Initialize the Core Formalism
 	String core_model
@@ -904,7 +941,7 @@ Void function user_function_skip_init(user_id : String):
 
 							// Also output the resulting model
 							output("Mapping success")
-							output(pretty_print(get_full_model(get_model_id(rendered_name))))
+							output(JSON_print(get_full_model(get_model_id(rendered_name))))
 
 						else:
 							output("Permission denied")