Skip to content

Commit

Permalink
Merge pull request #232 from odd/json-field-names
Browse files Browse the repository at this point in the history
Make json field names configurable.
  • Loading branch information
RuedigerMoeller authored Jan 3, 2018
2 parents 76e6499 + bc1a5e2 commit 40f1b01
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 44 deletions.
10 changes: 9 additions & 1 deletion src/main/java/org/nustaq/serialization/FSTConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.io.IOContext;
import com.fasterxml.jackson.core.json.JsonWriteContext;
import com.fasterxml.jackson.core.json.UTF8JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import org.nustaq.offheap.bytez.onheap.HeapBytez;
Expand Down Expand Up @@ -109,6 +108,15 @@ public interface ClassSecurityVerifier {

boolean forceClzInit = false; // always execute default fields init, even if no transients

FSTJsonFieldNames jsonFieldNames = new FSTJsonFieldNames("typ", "obj", "styp", "seq", "enum", "val", "ref");

public FSTJsonFieldNames getJsonFieldNames() {
return jsonFieldNames;
}
public void setJsonFieldNames(final FSTJsonFieldNames fieldNames) {
this.jsonFieldNames = fieldNames;
}

public ClassSecurityVerifier getVerifier() {
return verifier;
}
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/nustaq/serialization/coders/FSTJsonDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class FSTJsonDecoder implements FSTDecoder {

protected FSTConfiguration conf;
protected FSTJsonFieldNames fieldNames;

protected JsonParser input;
protected JsonFactory fac;
Expand All @@ -30,13 +31,14 @@ public class FSTJsonDecoder implements FSTDecoder {
protected int unknownNestLevel;

public FSTJsonDecoder(FSTConfiguration conf) {
this.conf = conf;
fac = conf.getCoderSpecific();
setConf(conf);
}

@Override
public void setConf(FSTConfiguration conf) {
this.conf = conf;
fieldNames = conf.getJsonFieldNames();
}

@Override
Expand Down Expand Up @@ -345,11 +347,11 @@ public byte readObjectHeaderTag() throws IOException {
}

String typeTag = input.nextFieldName();
if ( typeTag.equals(FSTJsonEncoder.TYPE) ) {
if ( typeTag.equals(fieldNames.TYPE) ) {
// object
String type = input.nextTextValue();
String valueTag = input.nextFieldName();
if ( ! FSTJsonEncoder.OBJ.equals(valueTag) ) {
if ( ! fieldNames.OBJ.equals(valueTag) ) {
throw new RuntimeException("expected value attribute for object of type:"+type);
}
if ( ! input.nextToken().isStructStart() ) {
Expand All @@ -361,13 +363,13 @@ public byte readObjectHeaderTag() throws IOException {
FSTUtil.<RuntimeException>rethrow(e);
}
return FSTObjectOutput.OBJECT;
} else if ( typeTag.equals(FSTJsonEncoder.SEQ_TYPE) ) {
} else if ( typeTag.equals(fieldNames.SEQ_TYPE) ) {
// sequence
String type = input.nextTextValue();
try {
lastDirectClass = classForName(conf.getClassForCPName(type));
String valueTag = input.nextFieldName();
if ( ! FSTJsonEncoder.SEQ.equals(valueTag) ) {
if ( ! fieldNames.SEQ.equals(valueTag) ) {
throw new RuntimeException("expected value attribute for object of type:"+type);
}
if ( ! input.nextToken().isStructStart() ) {
Expand All @@ -377,14 +379,14 @@ public byte readObjectHeaderTag() throws IOException {
FSTUtil.<RuntimeException>rethrow(e);
}
return FSTObjectOutput.ARRAY;
} else if ( typeTag.equals(FSTJsonEncoder.REF) ) {
} else if ( typeTag.equals(fieldNames.REF) ) {
return FSTObjectOutput.HANDLE;
} else if ( typeTag.equals(FSTJsonEncoder.ENUM) ) {
} else if ( typeTag.equals(fieldNames.ENUM) ) {
try {
String clName = input.nextTextValue();
Class aClass = classForName(conf.getClassForCPName(clName));
String valueTag = input.nextFieldName();
if ( ! FSTJsonEncoder.VAL.equals(valueTag) ) {
if ( ! fieldNames.VAL.equals(valueTag) ) {
throw new RuntimeException("expected value attribute for enum of type:"+clName);
}
String enumString = input.nextTextValue();
Expand Down Expand Up @@ -628,16 +630,16 @@ public Object readArrayHeader() throws Exception {
return null;
} else {
jsonToken = input.nextToken(); // seqType
if ( FSTJsonEncoder.TYPE.equals(input.getText())) {
if ( fieldNames.TYPE.equals(input.getText())) {
// object
type = input.nextTextValue();
String valueTag = input.nextFieldName();
if ( !FSTJsonEncoder.OBJ.equals(valueTag) ) {
if ( !fieldNames.OBJ.equals(valueTag) ) {
throw new RuntimeException("expected value attribute for object of type:"+type);
}
return classForName(conf.getClassForCPName(type));
}
if (!FSTJsonEncoder.SEQ_TYPE.equals(input.getText()) ) {
if (!fieldNames.SEQ_TYPE.equals(input.getText()) ) {
System.out.println(">" + input.getCurrentToken()+" "+input.getText());
input.nextToken();
System.out.println(">" + input.getCurrentToken()+" "+input.getText());
Expand Down
46 changes: 14 additions & 32 deletions src/main/java/org/nustaq/serialization/coders/FSTJsonEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonStreamContext;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.io.SerializedString;
import com.fasterxml.jackson.core.json.JsonWriteContext;
import org.nustaq.serialization.*;
import org.nustaq.serialization.util.FSTOutputStream;
import org.nustaq.serialization.util.FSTUtil;
Expand All @@ -15,44 +13,28 @@
import java.lang.reflect.Array;
import java.util.Map;

import static org.nustaq.serialization.FSTObjectOutput.STRING;

/**
* Created by ruedi on 20/05/15.
*
*/
public class FSTJsonEncoder implements FSTEncoder {

public static final String TYPE = "typ";
public static final String OBJ = "obj";
public static final String SEQ_TYPE = "styp";
public static final String SEQ = "seq";
public static final String ENUM = "enum";
public static final String VAL = "val";
public static final String REF = "ref";

public static final SerializedString TYPE_S = new SerializedString(TYPE);
public static final SerializedString OBJ_S = new SerializedString(OBJ);
public static final SerializedString SEQ_TYPE_S = new SerializedString(SEQ_TYPE);
public static final SerializedString SEQ_S = new SerializedString(SEQ);
public static final SerializedString ENUM_S = new SerializedString(ENUM);
public static final SerializedString VAL_S = new SerializedString(VAL);
public static final SerializedString REF_S = new SerializedString(REF);

JsonFactory fac;
FSTConfiguration conf;
FSTJsonFieldNames fieldNames;

protected JsonGenerator gen;
FSTOutputStream out;

public FSTJsonEncoder(FSTConfiguration conf) {
this.conf = conf;
fac = conf.getCoderSpecific();
setConf(conf);
}

@Override
public void setConf(FSTConfiguration conf) {
this.conf = conf;
fieldNames = conf.getJsonFieldNames();
}

@Override
Expand Down Expand Up @@ -246,7 +228,7 @@ public boolean writeTag(byte tag, Object infoOrObject, long somValue, Object toW
switch (tag) {
case FSTObjectOutput.HANDLE:
gen.writeStartObject();
gen.writeFieldName(REF_S);
gen.writeFieldName(fieldNames.REF_S);
gen.writeNumber(somValue);
gen.writeEndObject();
return true;
Expand Down Expand Up @@ -286,16 +268,16 @@ public boolean writeTag(byte tag, Object infoOrObject, long somValue, Object toW
break;
if ( clzInfo.getSer()!=null || clzInfo.isExternalizable() ) {
gen.writeStartObject();
gen.writeFieldName(TYPE_S);
gen.writeFieldName(fieldNames.TYPE_S);
writeSymbolicClazz(clzInfo, clzInfo.getClazz());
gen.writeFieldName(OBJ_S);
gen.writeFieldName(fieldNames.OBJ_S);
gen.writeStartArray();
} else
{
gen.writeStartObject();
gen.writeFieldName(TYPE_S);
gen.writeFieldName(fieldNames.TYPE_S);
writeSymbolicClazz(clzInfo,clzInfo.getClazz());
gen.writeFieldName(OBJ_S);
gen.writeFieldName(fieldNames.OBJ_S);
gen.writeStartObject();
}
break;
Expand All @@ -322,9 +304,9 @@ public boolean writeTag(byte tag, Object infoOrObject, long somValue, Object toW
return true;
} else {
gen.writeStartObject();
gen.writeFieldName(SEQ_TYPE_S);
gen.writeFieldName(fieldNames.SEQ_TYPE_S);
writeSymbolicClazz(null,clz);
gen.writeFieldName(SEQ_S);
gen.writeFieldName(fieldNames.SEQ_S);
gen.writeStartArray();
}
break;
Expand All @@ -341,9 +323,9 @@ public boolean writeTag(byte tag, Object infoOrObject, long somValue, Object toW
}
}
gen.writeStartObject();
gen.writeFieldName(ENUM_S);
gen.writeFieldName(fieldNames.ENUM_S);
writeSymbolicClazz(null,c);
gen.writeFieldName(VAL_S);
gen.writeFieldName(fieldNames.VAL_S);
gen.writeString(toWrite.toString());
gen.writeEndObject();
return true;
Expand All @@ -355,9 +337,9 @@ public boolean writeTag(byte tag, Object infoOrObject, long somValue, Object toW

private void writeUnkown(Unknown toWrite, FSTObjectOutput oout) throws IOException {
gen.writeStartObject();
gen.writeFieldName(TYPE_S);
gen.writeFieldName(fieldNames.TYPE_S);
gen.writeString(toWrite.getType());
gen.writeFieldName(OBJ_S);
gen.writeFieldName(fieldNames.OBJ_S);
if ( toWrite.isSequence() ) {
gen.writeStartArray();
for (Object o : toWrite.getItems()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.nustaq.serialization.coders;

import com.fasterxml.jackson.core.io.SerializedString;

public final class FSTJsonFieldNames {
public final String TYPE; // = "typ"
public final String OBJ; // = "obj";
public final String SEQ_TYPE; // = "styp"
public final String SEQ; // = "seq";
public final String ENUM; // = "enum";
public final String VAL; // = "val";
public final String REF; // = "ref";
public final SerializedString TYPE_S; // = new SerializedString(TYPE);
public final SerializedString OBJ_S; // = new SerializedString(OBJ);
public final SerializedString SEQ_TYPE_S; // = new SerializedString(SEQ_TYPE);
public final SerializedString SEQ_S; // = new SerializedString(SEQ);
public final SerializedString ENUM_S; // = new SerializedString(ENUM);
public final SerializedString VAL_S; // = new SerializedString(VAL);
public final SerializedString REF_S; // = new SerializedString(REF);

public FSTJsonFieldNames(final String TYPE, final String OBJ, final String SEQ_TYPE, final String SEQ, final String ENUM, final String VAL, final String REF) {
this.TYPE = TYPE;
this.OBJ = OBJ;
this.SEQ_TYPE = SEQ_TYPE;
this.SEQ = SEQ;
this.ENUM = ENUM;
this.VAL = VAL;
this.REF = REF;
this.TYPE_S = new SerializedString(TYPE);
this.OBJ_S = new SerializedString(OBJ);
this.SEQ_TYPE_S = new SerializedString(SEQ_TYPE);
this.SEQ_S = new SerializedString(SEQ);
this.ENUM_S = new SerializedString(ENUM);
this.VAL_S = new SerializedString(VAL);
this.REF_S = new SerializedString(REF);
}

public FSTJsonFieldNames TYPE(final String TYPE) {
return new FSTJsonFieldNames(TYPE, OBJ, SEQ_TYPE, SEQ, ENUM, VAL, REF);
}
public FSTJsonFieldNames OBJ(final String OBJ) {
return new FSTJsonFieldNames(TYPE, OBJ, SEQ_TYPE, SEQ, ENUM, VAL, REF);
}
public FSTJsonFieldNames SEQ_TYPE(final String SEQ_TYPE) {
return new FSTJsonFieldNames(TYPE, OBJ, SEQ_TYPE, SEQ, ENUM, VAL, REF);
}
public FSTJsonFieldNames SEQ(final String SEQ) {
return new FSTJsonFieldNames(TYPE, OBJ, SEQ_TYPE, SEQ, ENUM, VAL, REF);
}
public FSTJsonFieldNames ENUM(final String ENUM) {
return new FSTJsonFieldNames(TYPE, OBJ, SEQ_TYPE, SEQ, ENUM, VAL, REF);
}
public FSTJsonFieldNames VAL(final String VAL) {
return new FSTJsonFieldNames(TYPE, OBJ, SEQ_TYPE, SEQ, ENUM, VAL, REF);
}
public FSTJsonFieldNames REF(final String REF) {
return new FSTJsonFieldNames(TYPE, OBJ, SEQ_TYPE, SEQ, ENUM, VAL, REF);
}
public static final FSTJsonFieldNames DEFAULT = new FSTJsonFieldNames("typ", "obj", "styp", "seq", "enum", "val", "ref");
}
Empty file added src/test/json/JsonTypOrder.java
Empty file.

0 comments on commit 40f1b01

Please sign in to comment.