difftreelog
Add std.manifestJsonMinified()
in: master
It was added to jsonnet v0.18.0.
5 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -18,6 +18,8 @@
pub struct ManifestJsonOptions<'s> {
pub padding: &'s str,
pub mtype: ManifestType,
+ pub newline: &'s str,
+ pub key_val_sep: &'s str,
}
pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {
@@ -48,7 +50,7 @@
buf.push('[');
if !items.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -59,7 +61,7 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
@@ -68,7 +70,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
@@ -85,7 +87,7 @@
let fields = obj.fields();
if !fields.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -96,12 +98,12 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
- buf.push_str(": ");
+ buf.push_str(options.key_val_sep);
crate::push(
None,
|| format!("field <{}> manifestification", field.clone()),
@@ -114,7 +116,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth121 ("trace".into(), builtin_trace),121 ("trace".into(), builtin_trace),122 ("join".into(), builtin_join),122 ("join".into(), builtin_join),123 ("escapeStringJson".into(), builtin_escape_string_json),123 ("escapeStringJson".into(), builtin_escape_string_json),124 ("manifestJsonEx".into(), builtin_manifest_json_ex),124 ("manifestJsonExImpl".into(), builtin_manifest_json_ex_impl),125 ("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),125 ("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc),126 ("reverse".into(), builtin_reverse),126 ("reverse".into(), builtin_reverse),127 ("id".into(), builtin_id),127 ("id".into(), builtin_id),754 })754 })755}755}756756757fn builtin_manifest_json_ex(757fn builtin_manifest_json_ex_impl(758 context: Context,758 context: Context,759 _loc: Option<&ExprLocation>,759 _loc: Option<&ExprLocation>,760 args: &ArgsDesc,760 args: &ArgsDesc,761) -> Result<Val> {761) -> Result<Val> {762 parse_args!(context, "manifestJsonEx", args, 2, [762 parse_args!(context, "manifestJsonEx", args, 4, [763 0, value: ty!(any);763 0, value: ty!(any);764 1, indent: ty!(string) => Val::Str;764 1, indent: ty!(string) => Val::Str;765 2, newline: ty!(string) => Val::Str;766 3, key_val_sep: ty!(string) => Val::Str;765 ], {767 ], {766 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {768 Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions {767 padding: &indent,769 padding: &indent,768 mtype: ManifestType::Std,770 mtype: ManifestType::Std,771 newline: &newline,772 key_val_sep: &key_val_sep,769 })?.into()))773 })?.into()))770 })774 })771}775}crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -810,6 +810,14 @@
}
#[test]
+ fn json_minified() {
+ assert_json!(
+ r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,
+ r#""{\"a\":3,\"b\":4,\"c\":6}""#
+ );
+ }
+
+ #[test]
fn parse_json() {
assert_json!(
r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -451,6 +451,8 @@
&ManifestJsonOptions {
padding: "",
mtype: ManifestType::ToString,
+ newline: "\n",
+ key_val_sep: ": ",
},
)?
.into(),
@@ -535,6 +537,8 @@
} else {
ManifestType::Manifest
},
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
@@ -547,6 +551,8 @@
&ManifestJsonOptions {
padding: &" ".repeat(padding),
mtype: ManifestType::Std,
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/std.jsonnet
+++ b/crates/jrsonnet-stdlib/src/std.jsonnet
@@ -372,7 +372,11 @@
manifestJson(value):: std.manifestJsonEx(value, ' '),
- manifestJsonEx:: $intrinsic(manifestJsonEx),
+ manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),
+
+ manifestJsonExImpl:: $intrinsic(manifestJsonExImpl),
+
+ manifestJsonEx(value, indent, newline='\n', key_val_sep=': '):: std.manifestJsonExImpl(value, indent, newline, key_val_sep),
manifestYamlDocImpl:: $intrinsic(manifestYamlDocImpl),