Skip to content

Commit

Permalink
Merge branch 'master' into toml
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenamar-db authored Dec 12, 2024
2 parents 1752efe + a4fee0b commit 45ae6c4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
5 changes: 3 additions & 2 deletions sjsonnet/src/sjsonnet/BaseCharRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import upickle.core.{ArrVisitor, ObjVisitor}
class BaseCharRenderer[T <: upickle.core.CharOps.Output]
(out: T,
indent: Int = -1,
escapeUnicode: Boolean = false) extends JsVisitor[T, T]{
escapeUnicode: Boolean = false,
newline: Array[Char] = Array('\n')) extends JsVisitor[T, T]{
protected[this] val elemBuilder = new upickle.core.CharBuilder
protected[this] val unicodeCharBuilder = new upickle.core.CharBuilder()
def flushCharBuilder() = {
Expand Down Expand Up @@ -154,7 +155,7 @@ class BaseCharRenderer[T <: upickle.core.CharOps.Output]
else {
var i = indent * depth
elemBuilder.ensureLength(i + 1)
elemBuilder.appendUnsafe('\n')
elemBuilder.appendAll(newline, newline.length)
while(i > 0) {
elemBuilder.appendUnsafe(' ')
i -= 1
Expand Down
13 changes: 7 additions & 6 deletions sjsonnet/src/sjsonnet/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,18 @@ class PythonRenderer(out: Writer = new java.io.StringWriter(),
}

/** Renderer used by std.manifestJson, std.manifestJsonMinified, and std.manifestJsonEx */
case class MaterializeJsonRenderer(indent: Int = 4, escapeUnicode: Boolean = false, out: StringWriter = new StringWriter())
extends BaseCharRenderer(out, indent, escapeUnicode) {
case class MaterializeJsonRenderer(indent: Int = 4, escapeUnicode: Boolean = false, out: StringWriter = new StringWriter(), newline: String = "\n", keyValueSeparator: String = ": ")
extends BaseCharRenderer(out, indent, escapeUnicode, newline.toCharArray) {
private val newLineCharArray = newline.toCharArray
private val keyValueSeparatorCharArray = keyValueSeparator.toCharArray

override def visitArray(length: Int, index: Int) = new ArrVisitor[StringWriter, StringWriter] {
flushBuffer()
elemBuilder.append('[')

depth += 1
// account for rendering differences of whitespaces in ujson and jsonnet manifestJson
if (length == 0 && indent != -1) elemBuilder.append('\n') else renderIndent()
if (length == 0 && indent != -1) elemBuilder.appendAll(newLineCharArray, newLineCharArray.length) else renderIndent()
def subVisitor = MaterializeJsonRenderer.this
def visitValue(v: StringWriter, index: Int): Unit = {
flushBuffer()
Expand All @@ -208,12 +210,11 @@ case class MaterializeJsonRenderer(indent: Int = 4, escapeUnicode: Boolean = fal
elemBuilder.append('{')
depth += 1
// account for rendering differences of whitespaces in ujson and jsonnet manifestJson
if (length == 0 && indent != -1) elemBuilder.append('\n') else renderIndent()
if (length == 0 && indent != -1) elemBuilder.appendAll(newLineCharArray, newLineCharArray.length) else renderIndent()
def subVisitor = MaterializeJsonRenderer.this
def visitKey(index: Int) = MaterializeJsonRenderer.this
def visitKeyValue(s: Any): Unit = {
elemBuilder.append(':')
if (indent != -1) elemBuilder.append(' ')
elemBuilder.appendAll(keyValueSeparatorCharArray, keyValueSeparatorCharArray.length)
}
def visitValue(v: StringWriter, index: Int): Unit = {
commaBuffered = true
Expand Down
8 changes: 4 additions & 4 deletions sjsonnet/src/sjsonnet/Std.scala
Original file line number Diff line number Diff line change
Expand Up @@ -702,13 +702,13 @@ class Std {

private object ManifestJsonMinified extends Val.Builtin1("manifestJsonMinified", "v") {
def evalRhs(v: Val, ev: EvalScope, pos: Position): Val =
Val.Str(pos, Materializer.apply0(v, MaterializeJsonRenderer(indent = -1))(ev).toString)
Val.Str(pos, Materializer.apply0(v, new MaterializeJsonRenderer(indent = -1, newline = "", keyValueSeparator = ":"))(ev).toString)
}

private object ManifestJsonEx extends Val.Builtin2("manifestJsonEx", "value", "indent") {
def evalRhs(v: Val, i: Val, ev: EvalScope, pos: Position): Val =
private object ManifestJsonEx extends Val.Builtin4("manifestJsonEx", "value", "indent", "newline", "key_val_sep", Array(null, null, Val.Str(dummyPos, "\n"), Val.Str(dummyPos, ": "))) {
def evalRhs(v: Val, i: Val, newline: Val, keyValSep: Val, ev: EvalScope, pos: Position): Val =
Val.Str(pos, Materializer
.apply0(v, MaterializeJsonRenderer(indent = i.asString.length))(ev)
.apply0(v, MaterializeJsonRenderer(indent = i.asString.length, newline = newline.asString, keyValueSeparator = keyValSep.asString))(ev)
.toString)
}

Expand Down
14 changes: 7 additions & 7 deletions sjsonnet/test/resources/test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,13 @@ std.assertEqual(
|||
) &&

//std.assertEqual(
// std.manifestJsonEx(some_json, '', ' ', ' : '),
// '{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], '
// + '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], '
// + '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], '
// + '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }'
//) &&
std.assertEqual(
std.manifestJsonEx(some_json, '', ' ', ' : '),
'{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], '
+ '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], '
+ '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], '
+ '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }'
) &&

std.assertEqual(
std.manifestJsonMinified(some_json),
Expand Down
14 changes: 7 additions & 7 deletions sjsonnet/test/resources/test_suite/stdlib_native.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -724,13 +724,13 @@ std.assertEqual(
|||
) &&

//std.assertEqual(
// std.manifestJsonEx(some_json, '', ' ', ' : '),
// '{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], '
// + '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], '
// + '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], '
// + '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }'
//) &&
std.assertEqual(
std.manifestJsonEx(some_json, '', ' ', ' : '),
'{ "\\"" : null, "arr" : [ [ [ ] ] ], "emptyArray" : [ ], '
+ '"emptyObject" : { }, "objectInArray" : [ { "f" : 3 } ], '
+ '"x" : [ 1, 2, 3, true, false, null, "string\\nstring\\n" ], '
+ '"y" : { "a" : 1, "b" : 2, "c" : [ 1, 2 ] } }'
) &&

std.assertEqual(
std.manifestJsonMinified(some_json),
Expand Down

0 comments on commit 45ae6c4

Please sign in to comment.