Skip to content

Commit c0248e1

Browse files
committed
JSONPacker.escape
1 parent e9ed366 commit c0248e1

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

src/main/java/org/msgpack/packer/JSONPacker.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.OutputStream;
2222
import java.nio.ByteBuffer;
2323
import java.math.BigInteger;
24+
import org.json.simple.JSONValue;
2425
import org.msgpack.io.Output;
2526
import org.msgpack.io.StreamOutput;
2627
import org.msgpack.MessagePack;
@@ -130,19 +131,19 @@ public void writeDouble(double v) throws IOException {
130131
}
131132

132133
public void writeByteArray(byte[] b, int off, int len) throws IOException {
133-
beginElement();
134+
beginStringElement();
134135
out.writeByte(QUOTE);
135-
out.write(b, off, len); // FIXME escape
136+
escape(out, b, off, len);
136137
out.writeByte(QUOTE);
137138
endElement();
138139
}
139140

140141
public void writeByteBuffer(ByteBuffer bb) throws IOException {
141-
beginElement();
142+
beginStringElement();
142143
out.writeByte(QUOTE);
143144
int pos = bb.position();
144145
try {
145-
out.write(bb); // FIXME escape
146+
escape(out, bb);
146147
} finally {
147148
bb.position(pos);
148149
}
@@ -151,10 +152,9 @@ public void writeByteBuffer(ByteBuffer bb) throws IOException {
151152
}
152153

153154
public void writeString(String s) throws IOException {
154-
beginElement();
155-
byte[] b = s.getBytes();
155+
beginStringElement();
156156
out.writeByte(QUOTE);
157-
out.write(b, 0, b.length); // FIXME escape
157+
escape(out, s);
158158
out.writeByte(QUOTE);
159159
endElement();
160160
}
@@ -214,6 +214,14 @@ public void writeMapEnd(boolean check) throws IOException {
214214
}
215215

216216
private void beginElement() throws IOException {
217+
int flag = flags[stack.getDepth()];
218+
if((flag & FLAG_MAP_KEY) != 0) {
219+
throw new IOException("Key of a map must be a string in JSON");
220+
}
221+
beginStringElement();
222+
}
223+
224+
private void beginStringElement() throws IOException {
217225
int flag = flags[stack.getDepth()];
218226
if((flag & FLAG_MAP_VALUE) != 0) {
219227
out.writeByte(COLON);
@@ -239,5 +247,24 @@ private void endElement() throws IOException {
239247
public void close() throws IOException {
240248
out.close();
241249
}
250+
251+
private static void escape(Output out, byte[] b, int off, int len) throws IOException {
252+
// TODO optimize
253+
escape(out, new String(b, off, len));
254+
}
255+
256+
private static void escape(Output out, ByteBuffer bb) throws IOException {
257+
// TODO optimize
258+
byte[] b = new byte[bb.remaining()];
259+
bb.get(b);
260+
escape(out, new String(b));
261+
}
262+
263+
private static void escape(Output out, String s) throws IOException {
264+
// TODO optimize
265+
String e = JSONValue.escape(s);
266+
byte[] raw = e.getBytes();
267+
out.write(raw, 0, raw.length);
268+
}
242269
}
243270

0 commit comments

Comments
 (0)