Skip to content

Commit

Permalink
deserialize Integer[] support input single integer, for issue #2593
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 17, 2024
1 parent 7ea0d82 commit 9f87113
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
throw new JSONException(jsonReader.info("not support input " + str));
}

if (jsonReader.isNumber()) {
return new Integer[]{
jsonReader.readInt32()
};
}

throw new JSONException(jsonReader.info("TODO"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public Object readObject(JSONReader jsonReader, Type fieldType, Object fieldName
throw new JSONException(jsonReader.info("not support input " + str));
}

if (jsonReader.isNumber()) {
return new Long[]{
jsonReader.readInt64()
};
}

throw new JSONException(jsonReader.info("TODO"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.alibaba.fastjson2.issues_2500;

import com.alibaba.fastjson2.JSON;
import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class Issue2593 {
@Test
public void test() {
String str = "{\"test\":[{\"demo\": 1}]}";
Bean bean = JSON.parseObject(str, Bean.class);
List<Item> items = bean.getTest();
assertEquals(1, items.size());
assertEquals(1, items.get(0).demo.length);
assertEquals(1, items.get(0).demo[0]);
}

@Data
static class Bean {
List<Item> test;
}

@Data
static class Item {
private Integer[] demo;
}

@Test
public void test1() {
Long[] longs = JSON.parseObject("123", Long[].class);
assertEquals(1, longs.length);
assertEquals(123L, longs[0]);
}
}

1 comment on commit 9f87113

@15102552479
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请问fastjson2中,JSON.toJSONString怎么时间格式转?

Please sign in to comment.