Skip to content

Commit

Permalink
fix JSONType & JSONField WriteNonStringValueAsString feature not work…
Browse files Browse the repository at this point in the history
…, for issue #2431
  • Loading branch information
wenshao committed Apr 13, 2024
1 parent bc167b0 commit 736a330
Show file tree
Hide file tree
Showing 26 changed files with 947 additions and 85 deletions.
193 changes: 193 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,199 @@ public final void writeString(Reader reader) {

public abstract void writeString(String str);

/**
* write short value as String
* @param value value
* @since 2.0.49
*/
public abstract void writeString(boolean value);

/**
* write short value as String
* @param value value
* @since 2.0.49
*/
public abstract void writeString(byte value);

/**
* write short value as String
* @param value value
* @since 2.0.49
*/
public abstract void writeString(short value);

/**
* write boolean array value as String
* @param value value
* @since 2.0.49
*/
public void writeString(boolean[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeString(value[i]);
}
endArray();
}

/**
* write byte array value as String
* @param value value
* @since 2.0.49
*/
public void writeString(byte[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeString(value[i]);
}
endArray();
}

/**
* write short array value as String
* @param value value
* @since 2.0.49
*/
public void writeString(short[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeString(value[i]);
}
endArray();
}

/**
* write int array value as String
* @param value value
* @since 2.0.49
*/
public void writeString(int[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeString(value[i]);
}
endArray();
}

/**
* write long array value as String
* @param value value
* @since 2.0.49
*/
public void writeString(long[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeString(value[i]);
}
endArray();
}

/**
* write float array value as String
* @param value value
* @since 2.0.49
*/
public void writeString(float[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeString(value[i]);
}
endArray();
}

/**
* write double array value as String
* @param value value
* @since 2.0.49
*/
public void writeString(double[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray();
for (int i = 0; i < value.length; i++) {
if (i != 0) {
writeComma();
}
writeString(value[i]);
}
endArray();
}

/**
* write int value as String
* @param value value
* @since 2.0.49
*/
public abstract void writeString(int value);

/**
* write float value as String
* @param value value
* @since 2.0.49
*/
public void writeString(float value) {
writeString(Float.toString(value));
}

/**
* write double value as String
* @param value value
* @since 2.0.49
*/
public void writeString(double value) {
writeString(Double.toString(value));
}

/**
* write long value as String
* @param value value
* @since 2.0.49
*/
public abstract void writeString(long value);

public abstract void writeStringLatin1(byte[] value);

public abstract void writeStringUTF16(byte[] value);
Expand Down
119 changes: 114 additions & 5 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,115 @@ protected void write0(char ch) {
throw new JSONException("unsupported operation");
}

@Override
public void writeString(boolean value) {
writeString(Boolean.toString(value));
}

@Override
public void writeString(byte value) {
writeString(Integer.toString(value));
}

@Override
public void writeString(short value) {
writeString(Integer.toString(value));
}

@Override
public void writeString(int value) {
writeString(Integer.toString(value));
}

@Override
public void writeString(long value) {
writeString(Long.toString(value));
}

@Override
public void writeString(boolean[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray(value.length);
for (int i = 0; i < value.length; i++) {
writeString(value[i]);
}
}

@Override
public void writeString(byte[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray(value.length);
for (int i = 0; i < value.length; i++) {
writeString(value[i]);
}
}

@Override
public void writeString(short[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray(value.length);
for (int i = 0; i < value.length; i++) {
writeString(value[i]);
}
}

@Override
public void writeString(int[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray(value.length);
for (int i = 0; i < value.length; i++) {
writeString(value[i]);
}
}

@Override
public void writeString(long[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray(value.length);
for (int i = 0; i < value.length; i++) {
writeString(value[i]);
}
}

@Override
public void writeString(float[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray(value.length);
for (int i = 0; i < value.length; i++) {
writeString(value[i]);
}
}

@Override
public void writeString(double[] value) {
if (value == null) {
writeArrayNull();
return;
}
startArray(value.length);
for (int i = 0; i < value.length; i++) {
writeString(value[i]);
}
}

@Override
public void writeString(char[] chars, int off, int len, boolean quote) {
if (chars == null) {
Expand Down Expand Up @@ -1961,15 +2070,15 @@ public void writeBool(boolean value) {
}

@Override
public void writeBool(boolean[] valeus) {
if (valeus == null) {
public void writeBool(boolean[] values) {
if (values == null) {
writeNull();
return;
}

startArray(valeus.length);
for (int i = 0; i < valeus.length; i++) {
writeBool(valeus[i]);
startArray(values.length);
for (int i = 0; i < values.length; i++) {
writeBool(values[i]);
}
endArray();
}
Expand Down
Loading

0 comments on commit 736a330

Please sign in to comment.