summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/config_tree.ml42
-rw-r--r--src/config_tree.mli4
-rw-r--r--vyos1x-config.opam2
3 files changed, 47 insertions, 1 deletions
diff --git a/src/config_tree.ml b/src/config_tree.ml
index 463eb4b..31c0d76 100644
--- a/src/config_tree.ml
+++ b/src/config_tree.ml
@@ -201,6 +201,44 @@ struct
end (* Renderer *)
+module JSONRenderer = struct
+ let render_values values =
+ match values with
+ | [] -> Printf.sprintf "{}"
+ | [v] -> Printf.sprintf "\"%s\"" (String.escaped v)
+ | _ ->
+ let rendered = List.map (fun s -> Printf.sprintf "\"%s\"" (String.escaped s)) values in
+ let rendered = String.concat "," rendered in
+ Printf.sprintf "[%s]" rendered
+
+ let rec render_node node =
+ let name = Vytree.name_of_node node in
+ let children = Vytree.children_of_node node in
+ let data = Vytree.data_of_node node in
+ match children, data.values with
+ | [], [] ->
+ (* Empty node.
+ In JSON, we don't differentiate between leaf and non-leaf nodes in this case. *)
+ Printf.sprintf "\"%s\": {}" name
+ | _, [] ->
+ (* Non-empty, non-leaf node. *)
+ let children_strs = List.map render_node children in
+ let children_str = String.concat "," children_strs in
+ Printf.sprintf "\"%s\": {%s}" name children_str
+ | [], _ ->
+ (* Leaf node with children. *)
+ Printf.sprintf "\"%s\": %s" name (render_values data.values)
+ | _, _ ->
+ (* Shouldn't happen *)
+ failwith "Internal error: non-leaf node with values"
+
+ let render_json node =
+ let children = Vytree.children_of_node node in
+ let child_configs = List.map render_node children in
+ let child_configs = String.concat "," child_configs in
+ Printf.sprintf "{%s}" child_configs
+end (* JSONRenderer *)
+
let render_commands node path =
let node =
match path with
@@ -212,3 +250,7 @@ let render_commands node path =
String.concat "\n" commands
let render_config = Renderer.render_config
+
+let render_json = JSONRenderer.render_json
+
+let render_json_ast c = to_yojson c |> Yojson.Safe.to_string
diff --git a/src/config_tree.mli b/src/config_tree.mli
index c01e299..730b32f 100644
--- a/src/config_tree.mli
+++ b/src/config_tree.mli
@@ -36,3 +36,7 @@ val is_tag : t -> string list -> bool
val render_commands : t -> string list -> string
val render_config : t -> string
+
+val render_json : t -> string
+
+val render_json_ast : t -> string
diff --git a/vyos1x-config.opam b/vyos1x-config.opam
index 8da2479..589f918 100644
--- a/vyos1x-config.opam
+++ b/vyos1x-config.opam
@@ -1,6 +1,6 @@
opam-version: "2.0"
name: "vyos1x-config"
-version: "0.1"
+version: "0.2"
synopsis: "VyOS 1.x and EdgeOS config file manipulation library"
description: """
A library for parsing, manipulating, and exporting VyOS 1.x and EdgeOS config files.