Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
419 changes: 248 additions & 171 deletions src/main/java/de/marhali/json5/Json5Array.java

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/main/java/de/marhali/json5/Json5Boolean.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public final class Json5Boolean extends Json5Primitive {
public Json5Boolean(Boolean value) {
super(value);
}

@Override
public Json5Element deepCopy() {
Json5Boolean o = new Json5Boolean((Boolean) value);
o.setComment(getComment());
return o;
}

@Override
public Json5Element noCommentCopy() {
return new Json5Boolean((Boolean) value);
}
}
92 changes: 89 additions & 3 deletions src/main/java/de/marhali/json5/Json5Element.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,54 @@
* @author Joel Leitch
*/
public abstract class Json5Element {
private String comment;

/**
* Gets the comment associated with this element.
*
* @return The comment string, or null if none exists.
*/
public String getComment() {
return this.comment;
}

/**
* 对于Json5Null对象的注释,请通过以下方法来set
* @see Json5Object#setComment(String, String)
* @see Json5Array#setComment(int, String)
* comment字段不会参与equals 和hashCode
*
* @param comment The comment string. Can be multi-line.
*/
public void setComment(String comment) {
// 不应该直接操作Json5Null.INSTANCE实例的注释
// 应该通过JsonObject或Json5Array的 setComment方法来操作
if (this == Json5Null.INSTANCE) {
return;
}
this.comment = comment;
}

/**
* Checks if a comment is associated with this element.
*
* @return True if a comment exists, false otherwise.
*/
public boolean hasComment() {
return this.comment != null;
}

/**
* @return A deep copy of this element. Immutable elements like primitives
* and nulls are not copied.
* @return 深拷贝对象,包含各个子元素的注释
*/
public abstract Json5Element deepCopy();

/**
*
* @return 返回一个不带任何注释的拷贝对象
*/
public abstract Json5Element noCommentCopy();

/**
* provides check for verifying if this element is an array or not.
*
Expand Down Expand Up @@ -76,6 +118,40 @@ public boolean isJson5Null() {
return this instanceof Json5Null;
}

/**
* 当对象为空字符串 空Json5Object 空Json5Array Json5Null时返回True,其它情况返回False.
*
* @return 对象是否为空
*/
public boolean isEmpty() {
if (this instanceof Json5Null) {
return true;
} else if (this instanceof Json5Object) {
// noinspection RedundantCast
return ((Json5Object) this).isEmpty();
} else if (this instanceof Json5Array) {
// noinspection RedundantCast
return ((Json5Array) this).isEmpty();
} else if (this instanceof Json5String) {
// noinspection RedundantCast
return ((Json5String) this).isEmpty();
}
return false;
}

/**
* 将本对象的注释复制到目标对象
*
* @param target 目标对象
* @see Json5Object#mergeCommentTo(Json5Object)
* @see Json5Array#mergeCommentTo(Json5Array)
*/
public void copyCommentTo(Json5Element target) {
if (target != null && this.comment != null) {
target.setComment(this.comment);
}
}

/**
* convenience method to get this element as a {@link Json5Object}. If the element is of some
* other type, a {@link IllegalStateException} will result. Hence it is best to use this method
Expand Down Expand Up @@ -133,6 +209,7 @@ public Json5Primitive getAsJson5Primitive() {
* @return get this element as a {@link Json5Null}.
* @throws IllegalStateException if the element is of another type.
*/
@SuppressWarnings("unused")
public Json5Null getAsJson5Null() {
if (isJson5Null()) {
return (Json5Null) this;
Expand Down Expand Up @@ -286,15 +363,25 @@ public short getAsShort() {
/**
* Returns a simple String representation of this element.
* For pretty-printing use {@link Json5Writer} with custom configuration options.
*
* @see #toString(Json5Options)
*/
@Override
public String toString() {
return toString(Json5Options.DEFAULT);
}

/**
* 将对象转为无注释标准json字符串
* @return 压缩的json字符串
*/
public String toStandardString() {
return toString(new Json5OptionsBuilder().notWriteComments().build());
}

/**
* Returns the String representation of this element.
*
* @param options Configured serialization behaviour
* @return Stringified representation of this element
*/
Expand All @@ -306,7 +393,6 @@ public String toString(Json5Options options) {
Json5Writer json5Writer = new Json5Writer(options, stringWriter);
json5Writer.write(this);
return stringWriter.toString();

} catch (IOException e) {
throw new AssertionError(e);
}
Expand Down
43 changes: 27 additions & 16 deletions src/main/java/de/marhali/json5/Json5Hexadecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@
* @author Marcel Haßlinger
*/
public final class Json5Hexadecimal extends Json5Primitive {
/**
* Creates a primitive containing a hex value.
*
* @param hex the value to create the primitive with.
*/
public Json5Hexadecimal(BigInteger hex) {
super(hex);
}

/**
* Creates a primitive containing a hex value. For String to Number conversion see {@link #parseHexString(String)}
*
* @param hex the value to create the primitive with.
*/
public Json5Hexadecimal(String hex) {
super(parseHexString(hex));
}

/**
* Converts the provided hex string into it's number representation.
Expand All @@ -49,36 +66,30 @@ public static BigInteger parseHexString(String hex) {
/**
* Converts the provided number into it's hex literal character representation.
*
* @param bigInteger the number value
* @param bigInteger the number value
* @param prefixPositive Prefix positive values with {@code +0x...} if true otherwise {@code 0x...}.
* @return Hex character string including prefix
*/
public static String serializeHexString(BigInteger bigInteger, boolean prefixPositive) {
Objects.requireNonNull(bigInteger);

if(bigInteger.signum() >= 0) {
if (bigInteger.signum() >= 0) {
return (prefixPositive ? "+0x" : "0x") + bigInteger.toString(16);
} else {
return "-0x" + bigInteger.abs().toString(16);
}
}

/**
* Creates a primitive containing a hex value.
*
* @param hex the value to create the primitive with.
*/
public Json5Hexadecimal(BigInteger hex) {
super(hex);
@Override
public Json5Element deepCopy() {
Json5Hexadecimal o = new Json5Hexadecimal((BigInteger) value);
o.setComment(getComment());
return o;
}

/**
* Creates a primitive containing a hex value. For String to Number conversion see {@link #parseHexString(String)}
*
* @param hex the value to create the primitive with.
*/
public Json5Hexadecimal(String hex) {
super(parseHexString(hex));
@Override
public Json5Element noCommentCopy() {
return new Json5Hexadecimal((BigInteger) value);
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/de/marhali/json5/Json5Null.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ public final class Json5Null extends Json5Element {
/**
* Constructor for internal use only. Use {@link #INSTANCE} instead.
*/
private Json5Null() {}
public Json5Null() { }

/**
* Returns the same instance since it is an immutable value
*/
@Override
public Json5Null deepCopy() {
Json5Null json5Null = new Json5Null();
json5Null.setComment(this.getComment());
return json5Null;
}

@Override
public Json5Element noCommentCopy() {
return INSTANCE;
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/de/marhali/json5/Json5Number.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public final class Json5Number extends Json5Primitive {
public Json5Number(Number number) {
super(number);
}

@Override
public Json5Element deepCopy() {
Json5Number o = new Json5Number((Number) value);
o.setComment(getComment());
return o;
}

@Override
public Json5Element noCommentCopy() {
return new Json5Number((Number) value);
}
}
Loading
Loading