Skip to content

Commit

Permalink
JSON serialization/deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
apangin committed Dec 26, 2018
1 parent a4589f3 commit 8c5c084
Show file tree
Hide file tree
Showing 49 changed files with 1,544 additions and 66 deletions.
5 changes: 5 additions & 0 deletions src/one/nio/serial/ArrayListSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,9 @@ public void toJson(ArrayList obj, StringBuilder builder) throws IOException {
}
builder.append(']');
}

@Override
public ArrayList fromJson(JsonReader in) throws IOException, ClassNotFoundException {
return in.readArray();
}
}
17 changes: 14 additions & 3 deletions src/one/nio/serial/BitSetSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

package one.nio.serial;

import one.nio.util.Base64;

import java.io.IOException;
import java.io.NotSerializableException;
import java.nio.LongBuffer;
import java.util.BitSet;

Expand Down Expand Up @@ -57,7 +58,17 @@ public void skip(DataStream in) throws IOException {
}

@Override
public void toJson(BitSet obj, StringBuilder builder) throws NotSerializableException {
throw new NotSerializableException(cls.getName());
public void toJson(BitSet obj, StringBuilder builder) {
Json.appendBinary(builder, obj.toByteArray());
}

@Override
public BitSet fromJson(JsonReader in) throws IOException {
return BitSet.valueOf(in.readBinary());
}

@Override
public BitSet fromString(String s) {
return BitSet.valueOf(Base64.decodeFromChars(s.toCharArray()));
}
}
20 changes: 20 additions & 0 deletions src/one/nio/serial/BooleanArraySerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package one.nio.serial;

import java.io.IOException;
import java.util.Arrays;

class BooleanArraySerializer extends Serializer<boolean[]> {
private static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
Expand Down Expand Up @@ -70,4 +71,23 @@ public void toJson(boolean[] obj, StringBuilder builder) {
}
builder.append(']');
}

@Override
public boolean[] fromJson(JsonReader in) throws IOException {
boolean[] result = new boolean[10];
int count = 0;

in.expect('[', "Expected array");
for (boolean needComma = false; in.skipWhitespace() != ']'; needComma = true) {
if (needComma) {
in.expect(',', "Unexpected end of array");
in.skipWhitespace();
}
if (count >= result.length) result = Arrays.copyOf(result, count * 2);
result[count++] = in.readBoolean();
}
in.read();

return Arrays.copyOf(result, count);
}
}
10 changes: 10 additions & 0 deletions src/one/nio/serial/BooleanSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public void skip(DataStream in) throws IOException {
public void toJson(Boolean v, StringBuilder builder) {
builder.append(v.booleanValue());
}

@Override
public Boolean fromJson(JsonReader in) throws IOException {
return in.readBoolean();
}

@Override
public Boolean fromString(String s) {
return Boolean.valueOf(s);
}
}
9 changes: 6 additions & 3 deletions src/one/nio/serial/ByteArraySerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package one.nio.serial;

import one.nio.util.Base64;

import java.io.IOException;

class ByteArraySerializer extends Serializer<byte[]> {
Expand Down Expand Up @@ -59,6 +57,11 @@ public void skip(DataStream in) throws IOException {

@Override
public void toJson(byte[] obj, StringBuilder builder) {
builder.append('"').append(Base64.encodeToChars(obj)).append('"');
Json.appendBinary(builder, obj);
}

@Override
public byte[] fromJson(JsonReader in) throws IOException {
return in.readBinary();
}
}
10 changes: 10 additions & 0 deletions src/one/nio/serial/ByteSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public void skip(DataStream in) throws IOException {
public void toJson(Byte obj, StringBuilder builder) {
builder.append(obj.byteValue());
}

@Override
public Byte fromJson(JsonReader in) throws IOException {
return in.readByte();
}

@Override
public Byte fromString(String s) {
return Byte.valueOf(s);
}
}
5 changes: 5 additions & 0 deletions src/one/nio/serial/CharacterArraySerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public void skip(DataStream in) throws IOException {
public void toJson(char[] obj, StringBuilder builder) {
Json.appendChars(builder, obj);
}

@Override
public char[] fromJson(JsonReader in) throws IOException {
return in.readString().toCharArray();
}
}
10 changes: 10 additions & 0 deletions src/one/nio/serial/CharacterSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ public void skip(DataStream in) throws IOException {
public void toJson(Character v, StringBuilder builder) {
Json.appendChar(builder, v);
}

@Override
public Character fromJson(JsonReader in) throws IOException {
return in.readChar();
}

@Override
public Character fromString(String s) {
return s.charAt(0);
}
}
31 changes: 31 additions & 0 deletions src/one/nio/serial/ClassSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package one.nio.serial;

import one.nio.gen.BytecodeGenerator;
import one.nio.util.Utf8;

import java.io.IOException;
Expand Down Expand Up @@ -60,4 +61,34 @@ public void skip(DataStream in) throws IOException {
public void toJson(Class<?> obj, StringBuilder builder) {
builder.append('"').append(obj.getName()).append('"');
}

@Override
public Class<?> fromJson(JsonReader in) throws IOException, ClassNotFoundException {
return fromString(in.readString());
}

@Override
public Class<?> fromString(String s) throws ClassNotFoundException {
switch (s) {
case "int":
return int.class;
case "long":
return long.class;
case "boolean":
return boolean.class;
case "byte":
return byte.class;
case "short":
return short.class;
case "char":
return char.class;
case "float":
return float.class;
case "double":
return double.class;
case "void":
return void.class;
}
return Class.forName(s, false, BytecodeGenerator.INSTANCE);
}
}
5 changes: 5 additions & 0 deletions src/one/nio/serial/CollectionSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public void toJson(Collection obj, StringBuilder builder) throws IOException {
builder.append(']');
}

@Override
public Collection fromJson(JsonReader in) throws IOException, ClassNotFoundException {
return in.readArray();
}

@SuppressWarnings("unchecked")
private Constructor findConstructor() {
try {
Expand Down
Loading

0 comments on commit 8c5c084

Please sign in to comment.