--- 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 { @@ -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 { --- a/crates/jrsonnet-evaluator/src/builtin/mod.rs +++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs @@ -121,7 +121,7 @@ ("trace".into(), builtin_trace), ("join".into(), builtin_join), ("escapeStringJson".into(), builtin_escape_string_json), - ("manifestJsonEx".into(), builtin_manifest_json_ex), + ("manifestJsonExImpl".into(), builtin_manifest_json_ex_impl), ("manifestYamlDocImpl".into(), builtin_manifest_yaml_doc), ("reverse".into(), builtin_reverse), ("id".into(), builtin_id), @@ -754,18 +754,22 @@ }) } -fn builtin_manifest_json_ex( +fn builtin_manifest_json_ex_impl( context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc, ) -> Result { - parse_args!(context, "manifestJsonEx", args, 2, [ + parse_args!(context, "manifestJsonEx", args, 4, [ 0, value: ty!(any); 1, indent: ty!(string) => Val::Str; + 2, newline: ty!(string) => Val::Str; + 3, key_val_sep: ty!(string) => Val::Str; ], { Ok(Val::Str(manifest_json_ex(&value, &ManifestJsonOptions { padding: &indent, mtype: ManifestType::Std, + newline: &newline, + key_val_sep: &key_val_sep, })?.into())) }) } --- 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": []}')"#, --- 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()) --- 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),