Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Fixed errors, added exception field/byte character feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJeongHwan committed Sep 17, 2019
1 parent b65b000 commit 10e57f2
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 24 deletions.
16 changes: 11 additions & 5 deletions src/main/java/com/realtimetech/kson/KsonContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
import java.util.List;
import java.util.Map;

import com.realtimetech.kson.annotation.ExceptionField;
import com.realtimetech.kson.annotation.PrimaryKeyField;
import com.realtimetech.kson.element.KsonArray;
import com.realtimetech.kson.element.KsonObject;
import com.realtimetech.kson.element.KsonValue;
import com.realtimetech.kson.primary.PrimaryKey;
import com.realtimetech.kson.stack.FastStack;
import com.realtimetech.kson.string.StringMaker;
import com.realtimetech.kson.transform.Transformer;
Expand Down Expand Up @@ -153,15 +154,15 @@ private Field[] getAccessibleFields(Class<?> clazz) {
if (clazz.getSuperclass() != null && clazz.getSuperclass() != Object.class) {
Field[] superFields = getAccessibleFields(clazz.getSuperclass());
for (Field superField : superFields) {
if (!fields.contains(superField)) {
if (!fields.contains(superField) && !superField.isAnnotationPresent(ExceptionField.class)) {
fields.add(superField);
}
}
}

for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
if (!fields.contains(field)) {
if (!fields.contains(field) && !field.isAnnotationPresent(ExceptionField.class)) {
fields.add(field);
}
}
Expand Down Expand Up @@ -223,7 +224,7 @@ public Field getPrimaryKeyField(Class<?> type) {
boolean matched = false;

for (Field field : this.getAccessibleFields(type)) {
if (field.isAnnotationPresent(PrimaryKey.class)) {
if (field.isAnnotationPresent(PrimaryKeyField.class)) {
this.primaryKeys.put(type, field);
matched = true;
break;
Expand Down Expand Up @@ -567,7 +568,7 @@ public KsonValue fromString(String kson) throws IOException {
this.stringMaker.add(currentChar);
}
} else if (currentMode == ValueMode.NUMBER) {
if (!(currentChar >= '0' && currentChar <= '9') && currentChar != '-' && currentChar != 'D' && currentChar != 'd' && currentChar != 'F' && currentChar != 'f' && currentChar != 'L' && currentChar != 'l' && currentChar != '.') {
if (!(currentChar >= '0' && currentChar <= '9') && currentChar != '-' && currentChar != 'D' && currentChar != 'd' && currentChar != 'F' && currentChar != 'f' && currentChar != 'L' && currentChar != 'l' && currentChar != 'B' && currentChar != 'b' && currentChar != '.') {
modeStack.pop();

char last = this.stringMaker.last();
Expand All @@ -588,6 +589,11 @@ public KsonValue fromString(String kson) throws IOException {
this.stringMaker.remove();
this.valueStack.push(Long.parseLong(this.stringMaker.toString()));
break;
case 'b':
case 'B':
this.stringMaker.remove();
this.valueStack.push(Byte.parseByte(this.stringMaker.toString()));
break;
default:
if (decimal) {
double value = Double.parseDouble(this.stringMaker.toString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.realtimetech.kson.primary;
package com.realtimetech.kson.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface PrimaryKey {
public @interface ExceptionField {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.realtimetech.kson.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface PrimaryKeyField {

}
2 changes: 2 additions & 0 deletions src/main/java/com/realtimetech/kson/element/KsonValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ default public String toString(Object object, boolean useKsonStandard) {
return object + "D";
} else if (object instanceof Long) {
return object + "L";
} else if (object instanceof Byte) {
return object + "B";
} else {
return object.toString();
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/com/realtimetech/kson/test/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class MainTest {
public static void main(String[] args) throws Exception {
KsonContext ksonContext = new KsonContext();


{
String fieldTest;

Expand Down Expand Up @@ -73,8 +72,7 @@ public static void main(String[] args) throws Exception {
Long startTime = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
KsonObject fromObject = (KsonObject) ksonContext.fromObject(collectObject);
TestObject good = ksonContext.toObject(TestObject.class,
ksonContext.fromString(fromObject.toJsonString()));
TestObject good = ksonContext.toObject(TestObject.class, ksonContext.fromString(fromObject.toKsonString()));
KsonObject toObject = (KsonObject) ksonContext.fromObject(good);
}
Long endTime = System.currentTimeMillis();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/realtimetech/kson/test/Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.Random;

import com.realtimetech.kson.primary.PrimaryKey;
import com.realtimetech.kson.annotation.PrimaryKeyField;

public class Test {
@PrimaryKey
@PrimaryKeyField
private int id;

private int value1;
Expand Down
29 changes: 17 additions & 12 deletions src/main/java/com/realtimetech/kson/test/TestObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import java.util.List;
import java.util.Map;

import com.realtimetech.kson.annotation.ExceptionField;
import com.realtimetech.reflection.access.ArrayAccessor;

public class TestObject {
private Test test;

@ExceptionField
private byte[] bytes;

private Test[] testArray;

private int[] intArray;
Expand Down Expand Up @@ -48,11 +52,13 @@ public class TestObject {

public String validationObject(TestObject collectObject) throws IllegalArgumentException, IllegalAccessException {
for (Field field : collectObject.getClass().getDeclaredFields()) {
Object originalObject = field.get(this);
Object targetObject = field.get(collectObject);
if (!field.isAnnotationPresent(ExceptionField.class)) {
Object originalObject = field.get(this);
Object targetObject = field.get(collectObject);

if (!validation(originalObject, targetObject)) {
return field.getName();
if (!validation(originalObject, targetObject)) {
return field.getName();
}
}
}

Expand Down Expand Up @@ -144,6 +150,13 @@ public TestObject(Test test) {
this.testInterface = new TestInterfaceImpl();
this.testAbstract = new TestAbstractImpl();

this.bytes = new byte[255];
{
for (byte i = -128; i < Byte.MAX_VALUE; i++) {
this.bytes[i + 128] = i;
}
}

this.testInterfaceImplArray = new TestInterfaceImpl[2];
{
this.testInterfaceImplArray[0] = new TestInterfaceImpl();
Expand Down Expand Up @@ -241,14 +254,6 @@ public TestObject(Test test) {
this.mapMap.put(keyMap, valueMap);
}
}

// this.maps = new HashMap[2];
//
// this.maps[0] = new HashMap<String, String>();
// this.maps[1] = new HashMap<String, String>();
//
// this.maps[0].put("Hello", "Hi");
// this.maps[1].put("Hello", "Hi");
}

public Test getTest() {
Expand Down

0 comments on commit 10e57f2

Please sign in to comment.