Skip to content

Commit

Permalink
20220101
Browse files Browse the repository at this point in the history
  • Loading branch information
yanxutao89 committed Jan 1, 2022
1 parent 42d3c76 commit 33c1244
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 40 deletions.
47 changes: 33 additions & 14 deletions yanson/src/main/java/yanson/json/JsonHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import yanson.utils.CollectionUtils;
import yanson.utils.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;

/**
* @Author: Yanxt7
Expand Down Expand Up @@ -125,7 +122,8 @@ public static String toJsonSting(Object object, StringBuilder json) {
if (object == null) {
json.append("null");
} else {
if (object.getClass().isArray() || object instanceof Collection) {
Class<?> objectClass = object.getClass();
if ((objectClass.isArray() && !objectClass.getComponentType().isPrimitive()) || object instanceof Collection) {
json.append(Constants.LEFT_SQUARE_BRACKET);
}
if (object instanceof Map) {
Expand All @@ -145,12 +143,33 @@ public static String toJsonSting(Object object, StringBuilder json) {
json.append(Constants.COMMA);
}
}
} else if (object.getClass().isArray()) {
Object[] array = (Object[]) object;
for (int i = 0; i < array.length; ++i) {
toJsonSting(array[i], json);
if (i < array.length - 1) {
json.append(Constants.COMMA);
} else if (objectClass.isArray()) {
if (object instanceof Object[]) {
Object[] array = (Object[]) object;
for (int i = 0; i < array.length; ++i) {
toJsonSting(array[i], json);
if (i < array.length - 1) {
json.append(Constants.COMMA);
}
}
} else if (objectClass.getComponentType().isPrimitive()) {
Class<?> componentType = objectClass.getComponentType();
if (componentType == boolean.class) {
json.append(Arrays.toString((boolean[])object));
} else if (componentType == byte.class) {
json.append(Arrays.toString((byte[])object));
} else if (componentType == char.class) {
json.append(Arrays.toString((char[]) object));
} else if (componentType == short.class) {
json.append(Arrays.toString((short[]) object));
} else if (componentType == int.class) {
json.append(Arrays.toString((int[]) object));
} else if (componentType == long.class) {
json.append(Arrays.toString((long[]) object));
} else if (componentType == float.class) {
json.append(Arrays.toString((float[]) object));
} else if (componentType == double.class) {
json.append(Arrays.toString((double[]) object));
}
}
} else if (object instanceof List) {
Expand All @@ -161,7 +180,7 @@ public static String toJsonSting(Object object, StringBuilder json) {
json.append(Constants.COMMA);
}
}
} else if (TypeUtil.isElementType(object.getClass())) {
} else if (TypeUtil.isElementType(objectClass)) {
if (object instanceof String) {
json.append(Constants.DOUBLE_QUOTATIONS);
}
Expand All @@ -170,7 +189,7 @@ public static String toJsonSting(Object object, StringBuilder json) {
json.append(Constants.DOUBLE_QUOTATIONS);
}
} else {
Map<String, Invoker> invokerMap = ClassUtil.getInvokerMap(object.getClass(), InvokerType.FIELD);
Map<String, Invoker> invokerMap = ClassUtil.getInvokerMap(objectClass, InvokerType.FIELD);
if (invokerMap.size() == 0) {
json.append("\"" + object.toString() + "\"");
} else {
Expand All @@ -184,7 +203,7 @@ public static String toJsonSting(Object object, StringBuilder json) {
if (object instanceof Map) {
json.append(Constants.RIGHT_CURLY_BRACKET);
}
if (object.getClass().isArray() || object instanceof Collection) {
if ((objectClass.isArray() && !objectClass.getComponentType().isPrimitive()) || object instanceof Collection) {
json.append(Constants.RIGHT_SQUARE_BRACKET);
}
}
Expand Down
31 changes: 5 additions & 26 deletions yanson/src/test/java/model/BaseTypeVo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class BaseTypeVo implements Serializable {
private List<EmployeeVo> employees;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date date;
private int[] ints = {1, 2, 3};

public String getString() {
return string;
Expand Down Expand Up @@ -78,33 +79,11 @@ public Date getDate() {
public void setDate(Date date) {
this.date = date;
}

@Override
public String toString() {
return "BaseTypeVo{" +
"string='" + string + '\'' +
", integer=" + integer +
", float2=" + float2 +
", boolean2=" + boolean2 +
", array=" + Arrays.toString(array) +
", employees=" + employees +
", date=" + date +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
BaseTypeVo that = (BaseTypeVo) o;
return integer == that.integer && Objects.equals(string, that.string) && Objects.equals(float2, that.float2) && Objects.equals(boolean2, that.boolean2) && Arrays.equals(array, that.array) && Objects.equals(employees, that.employees);
public int[] getInts() {
return ints;
}

@Override
public int hashCode() {
int result = Objects.hash(string, integer, float2, boolean2, employees);
result = 31 * result + Arrays.hashCode(array);
return result;
public void setInts(int[] ints) {
this.ints = ints;
}

}

0 comments on commit 33c1244

Please sign in to comment.