Skip to content

Commit

Permalink
Merge pull request #24 from devjeonghwan/main
Browse files Browse the repository at this point in the history
Fix recursive test and Add full stop in exception messages
  • Loading branch information
devjeonghwan authored Mar 5, 2022
2 parents 0886ce2 + 1ba1ce5 commit 3fba647
Show file tree
Hide file tree
Showing 15 changed files with 200 additions and 96 deletions.
2 changes: 1 addition & 1 deletion .idea/modules/opack.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/modules/opack.test.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/opack.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 22 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@ OpackValue opackValue = opacker.serialize(someObject);
#### 2. Deserialize
```java
Opacker opacker = new Opacker.Builder()
.setContextStackInitialSize(128) // (Optional) Creation size of stack for processing
.setValueStackInitialSize(512) // (Optional) Creation size of stack for processing
.setConvertEnumToOrdinal(false) // (Optional) Convert Enum to ordinal or name
.setEnableWrapListElementType(false) // (Optional) When converting elements of a list, record the type as well
.setEnableWrapMapElementType(false) // (Optional) When converting elements of a map, record the type as well
.setContextStackInitialSize(128) // (Optional) Creation size of stack for processing
.setValueStackInitialSize(512) // (Optional) Creation size of stack for processing

.setEnableWrapListElementType(false) // (Optional) When converting elements of a list, record the type as well
.setEnableWrapMapElementType(false) // (Optional) When converting elements of a map, record the type as well
.setEnableConvertEnumToOrdinal(false) // (Optional) Convert enum to ordinal or name
.setEnableConvertRecursiveDependencyToNull(false); // (Optional) Convert recursive depandency, record null

.create();

OpackValue serializedSomeObject = /** See Serialize Usage **/;
Expand All @@ -59,11 +62,14 @@ SomeObject someObject = opacker.deserialize(SomeObject.class, serializedSomeObje
#### 3. Json Codec
```java
JsonCodec jsonCodec = new JsonCodec.Builder()
.setEncodeStackInitialSize(128) // (Optional) Creation size of stack for processing
.setEncodeStringBufferSize(1024) // (Optional) Creation size of stack for processing
.setDecodeStackInitialSize(128) // (Optional) Creation size of stack for processing
.setAllowOpackValueToKeyValue(false) // (Optional) Accepts Objct or Array as Key of Json Object
.setPrettyFormat(false) // (Optional) When encoding, it prints formatted
.setEncodeStackInitialSize(128) // (Optional) Creation size of stack for processing
.setEncodeStringBufferSize(1024) // (Optional) Creation size of stack for processing
.setDecodeStackInitialSize(128) // (Optional) Creation size of stack for processing

.setAllowOpackValueToKeyValue(false) // (Optional) Accepts Objct or Array as Key of Json Object
.setEnableConvertCharacterToString(false) // (Optional) Convert character to string instead of character int value
.setUsePrettyFormat(false) // (Optional) When encoding, it prints formatted

.create();

OpackValue opackValue = /** See Serialize Usage **/;
Expand All @@ -85,10 +91,12 @@ OpackValue decodedOpackValue = jsonCodec.decode(json);
#### 4. Dense Codec
```java
DenseCodec denseCodec = new DenseCodec.Builder()
.setDecodeStackInitialSize(128) // (Optional) Creation size of stack for processing
.setEncodeStackInitialSize(128) // (Optional) Creation size of stack for processing
.setEncodeOutputBufferInitialSize(1024) // (Optional) Creation size of stack for processing
.setIgnoreVersionCompare(false) // (Optional) Ignore compare dense codec version in data
.setEncodeStackInitialSize(128) // (Optional) Creation size of stack for processing
.setEncodeOutputBufferInitialSize(1024) // (Optional) Creation size of stack for processing
.setDecodeStackInitialSize(128) // (Optional) Creation size of stack for processing

.setIgnoreVersionCompare(false) // (Optional) Ignore compare dense codec version in data

.create();

OpackValue opackValue = /** See Serialize Usage **/;
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ plugins {

group 'com.realtimetech'
archivesBaseName = "opack"
version = "0.0.7"
version = "0.0.8"

compileJava.options.encoding = 'UTF-8'

Expand Down
70 changes: 48 additions & 22 deletions src/main/java/com/realtimetech/opack/Opacker.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

Expand All @@ -52,17 +53,17 @@ public static class Builder {

boolean enableWrapListElementType;
boolean enableWrapMapElementType;

boolean convertEnumToOrdinal;
boolean enableConvertEnumToOrdinal;
boolean enableConvertRecursiveDependencyToNull;

public Builder() {
this.valueStackInitialSize = 512;
this.contextStackInitialSize = 128;

this.enableWrapListElementType = false;
this.enableWrapMapElementType = false;

this.convertEnumToOrdinal = false;
this.enableConvertEnumToOrdinal = false;
this.enableConvertRecursiveDependencyToNull = false;
}

public Builder setValueStackInitialSize(int valueStackInitialSize) {
Expand All @@ -85,8 +86,13 @@ public Builder setEnableWrapMapElementType(boolean enableWrapMapElementType) {
return this;
}

public Builder setConvertEnumToOrdinal(boolean convertEnumToOrdinal) {
this.convertEnumToOrdinal = convertEnumToOrdinal;
public Builder setEnableConvertEnumToOrdinal(boolean enableConvertEnumToOrdinal) {
this.enableConvertEnumToOrdinal = enableConvertEnumToOrdinal;
return this;
}

public Builder setEnableConvertRecursiveDependencyToNull(boolean enableConvertRecursiveDependencyToNull) {
this.enableConvertRecursiveDependencyToNull = enableConvertRecursiveDependencyToNull;
return this;
}

Expand All @@ -109,8 +115,10 @@ public enum State {
final @NotNull FastStack<Object> objectStack;
final @NotNull FastStack<BakedType> typeStack;
final @NotNull FastStack<OpackValue> valueStack;
final @NotNull HashSet<Object> overlapSet;

final boolean convertEnumToOrdinal;
final boolean enableConvertEnumToOrdinal;
final boolean enableConvertRecursiveDependencyToNull;

@NotNull State state;

Expand All @@ -126,7 +134,7 @@ public enum State {
this.objectStack = new FastStack<>(builder.contextStackInitialSize);
this.typeStack = new FastStack<>(builder.contextStackInitialSize);
this.valueStack = new FastStack<>(builder.valueStackInitialSize);

this.overlapSet = new HashSet<>();
this.state = State.NONE;

try {
Expand All @@ -145,7 +153,8 @@ public enum State {
throw new IllegalStateException(exception);
}

this.convertEnumToOrdinal = builder.convertEnumToOrdinal;
this.enableConvertEnumToOrdinal = builder.enableConvertEnumToOrdinal;
this.enableConvertRecursiveDependencyToNull = builder.enableConvertRecursiveDependencyToNull;
}

/**
Expand All @@ -157,7 +166,11 @@ public enum State {
*/
public synchronized OpackValue serialize(Object object) throws SerializeException {
if (this.state == State.DESERIALIZE)
throw new SerializeException("Opacker is deserializing");
throw new SerializeException("Opacker is deserializing.");

if (this.state == State.NONE) {
this.overlapSet.clear();
}

int separatorStack = this.objectStack.getSize();
OpackValue value = (OpackValue) this.prepareObjectSerialize(object.getClass(), object.getClass(), object);
Expand Down Expand Up @@ -216,7 +229,7 @@ Object prepareObjectSerialize(Class<?> baseType, Class<?> originalType, Object o
Enum converting
*/
if (objectType.isEnum()) {
if (this.convertEnumToOrdinal) {
if (this.enableConvertEnumToOrdinal) {
Object[] enums = objectType.getEnumConstants();

for (int i = 0; i < enums.length; i++) {
Expand Down Expand Up @@ -250,13 +263,22 @@ Object prepareObjectSerialize(Class<?> baseType, Class<?> originalType, Object o
opackValue = new OpackObject<>();
}

if (this.overlapSet.contains(object)) {
if (!this.enableConvertRecursiveDependencyToNull) {
throw new SerializeException("Recursive dependencies are not serializable.");
}

return null;
}

this.overlapSet.add(object);
this.objectStack.push(object);
this.valueStack.push(opackValue);
this.typeStack.push(bakedType);

return opackValue;
} catch (BakeException exception) {
throw new SerializeException("Can't bake " + baseType.getName() + " class information", exception);
throw new SerializeException("Can't bake " + baseType.getName() + " class information.", exception);
}
}

Expand Down Expand Up @@ -299,7 +321,7 @@ void executeSerializeStack(int endOfStack) throws SerializeException {
Object serializedValue = this.prepareObjectSerialize(fieldType, originalType, element);
opackObject.put(property.getName(), serializedValue);
} catch (IllegalAccessException exception) {
throw new SerializeException("Can't get " + property.getName() + " field data in " + bakedType.getType().getSimpleName(), exception);
throw new SerializeException("Can't get " + property.getName() + " field data in " + bakedType.getType().getSimpleName() + ".", exception);
}
}
}
Expand All @@ -316,7 +338,11 @@ void executeSerializeStack(int endOfStack) throws SerializeException {
*/
public synchronized <T> T deserialize(Class<T> type, OpackValue opackValue) throws DeserializeException {
if (this.state == State.SERIALIZE)
throw new DeserializeException("Opacker is serializing");
throw new DeserializeException("Opacker is serializing.");

if (this.state == State.NONE) {
this.overlapSet.clear();
}

int separatorStack = this.objectStack.getSize();
T value = type.cast(this.prepareObjectDeserialize(type, opackValue));
Expand Down Expand Up @@ -369,7 +395,7 @@ public synchronized Object prepareObjectDeserialize(Class<?> goalType, Object ob
Enum converting
*/
if (goalType.isEnum()) {
if (this.convertEnumToOrdinal) {
if (this.enableConvertEnumToOrdinal) {
return goalType.getEnumConstants()[(int) ReflectionUtil.cast(Integer.class, object)];
} else {
return Enum.valueOf((Class<? extends Enum>) goalType, object.toString());
Expand All @@ -389,7 +415,7 @@ public synchronized Object prepareObjectDeserialize(Class<?> goalType, Object ob
try {
return OpackArrayConverter.convertToArray(componentType, opackArray);
} catch (InvocationTargetException | IllegalAccessException exception) {
throw new DeserializeException("Can't convert OpackArray to native array", exception);
throw new DeserializeException("Can't convert OpackArray to native array.", exception);
}
}
}
Expand All @@ -405,7 +431,7 @@ public synchronized Object prepareObjectDeserialize(Class<?> goalType, Object ob

targetObject = Array.newInstance(goalType.getComponentType(), opackArray.length());
} else {
throw new DeserializeException("Target class is array. but, object is not OpackArray");
throw new DeserializeException("Target class is array. but, object is not OpackArray.");
}
} else {
if (object instanceof OpackObject) {
Expand All @@ -414,10 +440,10 @@ public synchronized Object prepareObjectDeserialize(Class<?> goalType, Object ob
try {
targetObject = ReflectionUtil.createInstanceUnsafe(goalType);
} catch (InvocationTargetException | IllegalAccessException | InstantiationException exception) {
throw new DeserializeException("Can't create instance using unsafe method", exception);
throw new DeserializeException("Can't create instance using unsafe method.", exception);
}
} else {
throw new DeserializeException("Target class is object. but, object is not OpackObject");
throw new DeserializeException("Target class is object. but, object is not OpackObject.");
}
}

Expand All @@ -429,10 +455,10 @@ public synchronized Object prepareObjectDeserialize(Class<?> goalType, Object ob
} else if (object.getClass() == goalType) {
return object;
} else {
throw new DeserializeException("Found object, stack corruption");
throw new DeserializeException("Found object, stack corruption.");
}
} catch (BakeException exception) {
throw new DeserializeException("Can't bake " + goalType.getName() + " class information", exception);
throw new DeserializeException("Can't bake " + goalType.getName() + " class information.", exception);
}
}

Expand Down Expand Up @@ -474,7 +500,7 @@ void executeDeserializeStack(int endOfStack) throws DeserializeException {

property.set(object, deserializedValue == null ? null : ReflectionUtil.cast(actualFieldType, deserializedValue));
} catch (IllegalAccessException | IllegalArgumentException exception) {
throw new DeserializeException("Can't set " + property.getName() + " field in " + bakedType.getType().getSimpleName(), exception);
throw new DeserializeException("Can't set " + property.getName() + " field in " + bakedType.getType().getSimpleName() + ".", exception);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public final static class Builder {
int encodeOutputBufferInitialSize;
int encodeStackInitialSize;
int decodeStackInitialSize;

boolean ignoreVersionCompare;

public Builder() {
Expand Down Expand Up @@ -365,7 +366,7 @@ protected void doEncode(OutputStream outputStream, OpackValue opackValue) throws
}
}
} catch (InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException("Failed to access the native list object in OpackArray");
throw new IllegalStateException("Failed to access the native list object in OpackArray.");
}
} else {
if (objectType == boolean.class) {
Expand Down Expand Up @@ -400,7 +401,7 @@ protected void doEncode(OutputStream outputStream, OpackValue opackValue) throws
denseWriter.writeInt(bytes.length);
denseWriter.writeBytes(bytes);
} else {
throw new IllegalArgumentException(objectType + " is not allowed in dense format. (unknown literal object type)");
throw new IllegalArgumentException(objectType + " is not allowed in dense format. (unknown literal object type).");
}
}
}
Expand Down
Loading

0 comments on commit 3fba647

Please sign in to comment.