diff options
author | jonmv <venstad@gmail.com> | 2022-04-10 14:48:33 +0200 |
---|---|---|
committer | jonmv <venstad@gmail.com> | 2022-04-11 13:42:26 +0200 |
commit | 9354bd3ea29851eed945c4bd61370629bb4d5598 (patch) | |
tree | 215186121e6a7519865ce818a010577f3bc73f85 /vespajlib | |
parent | 9273cb5861e1f8b258968ca85dbae31336e12d51 (diff) |
Make indent specifiable in JsonFormat
Diffstat (limited to 'vespajlib')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/slime/JsonFormat.java | 46 | ||||
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/slime/SlimeUtils.java | 6 |
2 files changed, 32 insertions, 20 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/slime/JsonFormat.java b/vespajlib/src/main/java/com/yahoo/slime/JsonFormat.java index 5532ccbda9c..a757ee8fa6d 100644 --- a/vespajlib/src/main/java/com/yahoo/slime/JsonFormat.java +++ b/vespajlib/src/main/java/com/yahoo/slime/JsonFormat.java @@ -1,6 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.slime; +import ai.vespa.validation.Validation; import com.yahoo.io.AbstractByteWriter; import com.yahoo.io.ByteWriter; import com.yahoo.text.AbstractUtf8Array; @@ -8,6 +9,10 @@ import com.yahoo.text.Utf8; import com.yahoo.text.Utf8String; import java.io.*; +import java.nio.charset.StandardCharsets; + +import static ai.vespa.validation.Validation.requireInRange; +import static java.nio.charset.StandardCharsets.UTF_8; /** * Encodes json from a slime object. @@ -17,26 +22,31 @@ import java.io.*; public final class JsonFormat implements SlimeFormat { private final static byte [] HEX = Utf8.toBytes("0123456789ABCDEF"); - private final boolean compact; + private final int indent; + + public JsonFormat(int indent) { + this.indent = requireInRange(indent, "JSON space indent count", 0, 8); + } + public JsonFormat(boolean compact) { - this.compact = compact; + this(compact ? 0 : 1); } @Override public void encode(OutputStream os, Slime slime) throws IOException { - new Encoder(slime.get(), os, compact).encode(); + new Encoder(slime.get(), os, indent).encode(); } public void encode(OutputStream os, Inspector value) throws IOException { - new Encoder(value, os, compact).encode(); + new Encoder(value, os, indent).encode(); } public void encode(AbstractByteWriter os, Slime slime) throws IOException { - new Encoder(slime.get(), os, compact).encode(); + new Encoder(slime.get(), os, indent).encode(); } public void encode(AbstractByteWriter os, Inspector value) throws IOException { - new Encoder(value, os, compact).encode(); + new Encoder(value, os, indent).encode(); } @Override @@ -65,31 +75,29 @@ public final class JsonFormat implements SlimeFormat { return slime; } - public static final class Encoder implements ArrayTraverser, ObjectTraverser { + static final class Encoder implements ArrayTraverser, ObjectTraverser { private final Inspector top; private final AbstractByteWriter out; + private final byte[] indent; private boolean head = true; - private boolean compact; private int level = 0; final static AbstractUtf8Array NULL=new Utf8String("null"); final static AbstractUtf8Array FALSE=new Utf8String("false"); final static AbstractUtf8Array TRUE=new Utf8String("true"); - public Encoder(Inspector value, OutputStream out, boolean compact) { - this.top = value; - this.out = new ByteWriter(out); - this.compact = compact; + public Encoder(Inspector value, OutputStream out, int indent) { + this(value, new ByteWriter(out), indent); } - public Encoder(Inspector value, AbstractByteWriter out, boolean compact) { + public Encoder(Inspector value, AbstractByteWriter out, int indent) { this.top = value; this.out = out; - this.compact = compact; + this.indent = indent == 0 ? null : " ".repeat(indent).getBytes(UTF_8); } public void encode() throws IOException { encodeValue(top); - if (!compact) { + if (indent != null) { out.append((byte) '\n'); } out.flush(); @@ -205,9 +213,9 @@ public final class JsonFormat implements SlimeFormat { } else { head = false; } - if (!compact) { + if (indent != null) { out.append((byte)'\n'); - for (int lvl = 0; lvl < level; lvl++) { out.append((byte)' '); } + for (int lvl = 0; lvl < level; lvl++) { out.append(indent); } } } @@ -226,8 +234,8 @@ public final class JsonFormat implements SlimeFormat { separate(true); encodeSTRING(Utf8Codec.encode(name)); out.append((byte)':'); - if (!compact) - out.append((byte)' '); + if (indent != null) + out.append((byte) ' '); encodeValue(inspector); } catch (Exception e) { // FIXME: Should we fix ArrayTraverser/ObjectTraverser API or do something more fancy here? diff --git a/vespajlib/src/main/java/com/yahoo/slime/SlimeUtils.java b/vespajlib/src/main/java/com/yahoo/slime/SlimeUtils.java index 5e0cafe6527..c36b001d056 100644 --- a/vespajlib/src/main/java/com/yahoo/slime/SlimeUtils.java +++ b/vespajlib/src/main/java/com/yahoo/slime/SlimeUtils.java @@ -106,8 +106,12 @@ public class SlimeUtils { } public static byte[] toJsonBytes(Inspector inspector) throws IOException { + return toJsonBytes(inspector, true); + } + + public static byte[] toJsonBytes(Inspector inspector, boolean compact) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); - new JsonFormat(true).encode(baos, inspector); + new JsonFormat(compact ? 0 : 2).encode(baos, inspector); return baos.toByteArray(); } |