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

Commit

Permalink
Merge pull request #16 from StaticDefault/master
Browse files Browse the repository at this point in the history
Added StringWriter.
  • Loading branch information
StaticDefaultTester2 authored Oct 4, 2019
2 parents 9bf83a6 + 1ab2e38 commit 95e4035
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 121 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/realtimetech/kson/KsonContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public UUID deserialize(KsonContext ksonContext, Class<?> object, Object value)
return UUID.fromString((String) value);
}
});

}

private Class<?> getClassByName(String name) throws ClassNotFoundException {
Expand Down
29 changes: 10 additions & 19 deletions src/main/java/com/realtimetech/kson/element/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,25 @@

import java.util.ArrayList;

import com.realtimetech.kson.writer.KsonWriter;

public class JsonArray extends ArrayList<Object> implements JsonValue {

/**
* 기본 Serial UID
*/
private static final long serialVersionUID = 5513748119461105760L;
private final StringBuffer stringBuffer = new StringBuffer();

protected KsonWriter ksonWriter = null;

@Override
public String toString(boolean useKsonStandard) {
boolean firstElement = true;

stringBuffer.setLength(0);

stringBuffer.append("[");
for (Object object : this) {
if (firstElement) {
firstElement = false;
stringBuffer.append(this.toString(object, useKsonStandard));
} else {
stringBuffer.append(",");
stringBuffer.append(this.toString(object, useKsonStandard));
}
}
stringBuffer.append("]");

return stringBuffer.toString();
if (ksonWriter == null)
this.ksonWriter = new KsonWriter();

this.ksonWriter.setUseKson(useKsonStandard);

return this.ksonWriter.toString(this);
}

@Override
Expand Down
31 changes: 8 additions & 23 deletions src/main/java/com/realtimetech/kson/element/JsonObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,25 @@

import java.util.HashMap;

import com.realtimetech.kson.writer.KsonWriter;

public class JsonObject extends HashMap<Object, Object> implements JsonValue {

/**
* 기본 Serial UID
*/
private static final long serialVersionUID = -6357620110797218097L;
private final StringBuffer stringBuffer = new StringBuffer();

protected KsonWriter ksonWriter = null;

@Override
public String toString(boolean useKsonStandard) {
boolean firstElement = true;
if (ksonWriter == null)
this.ksonWriter = new KsonWriter();

stringBuffer.setLength(0);
this.ksonWriter.setUseKson(useKsonStandard);

stringBuffer.append("{");
for (Object key : keySet()) {
Object value = get(key);

if (firstElement) {
firstElement = false;
stringBuffer.append(this.toString(key, useKsonStandard));
stringBuffer.append(":");
stringBuffer.append(this.toString(value, useKsonStandard));
} else {
stringBuffer.append(",");
stringBuffer.append(this.toString(key, useKsonStandard));
stringBuffer.append(":");
stringBuffer.append(this.toString(value, useKsonStandard));
}
}
stringBuffer.append("}");

return stringBuffer.toString();
return this.ksonWriter.toString(this);
}

@Override
Expand Down
78 changes: 0 additions & 78 deletions src/main/java/com/realtimetech/kson/element/JsonValue.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.realtimetech.kson.element;

import com.realtimetech.kson.util.string.StringMaker;

public interface JsonValue {
StringMaker stringMaker = new StringMaker();

default public String toKsonString() {
return toString(true);
}
Expand All @@ -13,79 +9,5 @@ default public String toJsonString() {
return toString(false);
}

public static String escape(String string) {
if (string == null) {
return null;
}
stringMaker.reset();

for (int i = 0; i < string.length(); i++) {
char character = string.charAt(i);
switch (character) {
case '"':
stringMaker.add('\\');
stringMaker.add('\"');
break;
case '\\':
stringMaker.add('\\');
stringMaker.add('\\');
break;
case '\b':
stringMaker.add('\\');
stringMaker.add('b');
break;
case '\f':
stringMaker.add('\\');
stringMaker.add('f');
break;
case '\n':
stringMaker.add('\\');
stringMaker.add('n');
break;
case '\r':
stringMaker.add('\\');
stringMaker.add('r');
break;
case '\t':
stringMaker.add('\\');
stringMaker.add('t');
break;
default:
stringMaker.add(character);
}
}

return stringMaker.toString();
}

default public String toString(Object object, boolean useKsonStandard) {
if (object == null)
return "null";

if (object instanceof JsonValue) {
JsonValue ksonValue = (JsonValue) object;
return ksonValue.toString(useKsonStandard);
} else if (object instanceof String) {
String string = (String) object;
return "\"" + escape(string) + "\"";
} else {
if (useKsonStandard) {
if (object instanceof Float) {
return object + "F";
} else if (object instanceof Double) {
return object + "D";
} else if (object instanceof Long) {
return object + "L";
} else if (object instanceof Byte) {
return object + "B";
} else {
return object.toString();
}
} else {
return object.toString();
}
}
}

public String toString(boolean useKsonStandard);
}
15 changes: 15 additions & 0 deletions src/main/java/com/realtimetech/kson/util/stack/FastStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public class FastStack<T> {

private int currentIndex;
private int scope;

private int startIndex;

public FastStack() {
this(10);
Expand All @@ -17,6 +19,7 @@ public FastStack() {
public FastStack(int raiseSize) {
this.raiseSize = raiseSize;
this.currentIndex = -1;
this.startIndex = 0;
this.scope = 0;

this.raiseArrays();
Expand Down Expand Up @@ -46,6 +49,10 @@ public void push(T object) {
this.objects[this.currentIndex] = object;
}

public T[] getArray() {
return objects;
}

public boolean isEmpty() {
return this.currentIndex == -1;
}
Expand All @@ -69,8 +76,16 @@ public T pop() {
return object;
}

public T shift() {
if (this.currentIndex == -1)
throw new EmptyStackException();

return this.objects[this.startIndex++];
}

public void reset() {
this.currentIndex = -1;
this.startIndex = 0;
}

public int getSize() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.realtimetech.kson.util.string;

import java.util.Arrays;

public class StringMaker {
private int raiseSize;

Expand Down Expand Up @@ -55,6 +57,10 @@ public void remove() {
this.currentIndex--;
}

public char[] toArray() {
return Arrays.copyOfRange(chars, 0, this.currentIndex + 1);
}

public String toString() {
return new String(chars, 0, this.currentIndex + 1);
}
Expand Down
Loading

0 comments on commit 95e4035

Please sign in to comment.