ããã§ä½ã£ãJSONãã¼ãµã«ãé常ã®ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ã¨ã®ç¸äºå¤ææ©è½ã追å ãã¾ããã
kmaebashi.hatenablog.com
ä¸è¨ã®æç¹ã§ã¯ãJSONã®ããã¹ããJsonElementã¨ããç¬èªã¯ã©ã¹(ã¤ã³ã¿ãã§ã¼ã¹)ã«ãã¼ã¹ãããã®ãµãã¤ã³ã¿ãã§ã¼ã¹ã®JsonElementãJsonObjectãJsonValueããå¤ãåãåºããã¨ããã¨ããã¾ã§ããå®è£
ãã¦ãã¾ããã§ããããã®å®è£
ã§ãã¾ãå½¹ã«ã¯ç«ã¤ã§ããããããä»»æã®JSONããã¼ã¹ããªããã°ãããªããã¨ããç¶æ³ã§ã¯ç¹ã«ä¾¿å©ã§ãããããããã¦ãã®ã±ã¼ã¹ã§ã¯JSONã§éã£ããåãåã£ãããããã¼ã¿å½¢å¼ã¯æ±ºã¾ã£ã¦ããã®ã§ãããç¨ã®ã¯ã©ã¹ãä½ã£ã¦ããã«ãããã³ã°ãã¦ãããæ¹ã便å©ã§ããå
ã®å®è£
ã§ã¯ãJsonObjectã¯Mapãä¿æãã¦ããã®ã§ããªãã¸ã§ã¯ãã®ããããã£ã®å¤ãåå¾ããããã«ã¯ãã¼ãæååã§æå®ããããã«ãªã£ã¦ããã®ã§ãããããã ã¨ãã¼ã®ååããã¹ã¿ã¤ããã¦ãã³ã³ãã¤ã«ã¨ã©ã¼ã«ãªã£ã¦ããã¾ãããç§ã¯ãåçåä»ãè¨èªã¨ããééã£ãã¢ã¤ãã£ã¢ã§ãã®æ¥çã¯20å¹´é åããããã¨æã£ã¦ãã人éãªã®ã§ããã¹ã¦ã®ãã°ã¯ã§ããã ãæ©ããå¯è½ãªãã³ã³ãã¤ã«ã®æ®µéã§æ©æ¢°ã«è¦ã¤ãã¦ã»ããã¨æã£ã¦ãã¾ãããã®æå³ã§ããéçåä»ãè¨èª(Java)ã®ã¯ã©ã¹ã«ãããã³ã°ããã»ããä»æ§ã¨ãã¦ãæ£ãããã¨æãã¾ãã
ä¿®æ£çã®ããã°ã©ã ã¯ä¾ã«ãã£ã¦GitHubã«ãä¸ãã¦ãã¾ãã
github.com
å
·ä½çãªä½¿ãæ¹ã¨ãã¦ã¯ããã¨ãã°ä»¥ä¸ã®ãããªã¯ã©ã¹Hogeããã£ãã¨ãã¦ã
public class Hoge {
public int intValue;
private int privateInt;
@JsonIgnore
public int intValueIgnore;
public Integer intObj;
public float floatValue;
public double doubleValue;
public boolean booleanValue;
public String stringValue;
public int[] intArray;
public Integer[] intObjArray;
public Hoge[] hogeArray;
public List<Hoge> hogeList;
public void setPrivateInt(int value) {
this.privateInt = value;
}
}
ãã®ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãJSONã«å¤æããã«ã¯ãClassMapper.toJson()ã¡ã½ããã使ãã¾ãã
Hoge hoge = new Hoge();
hoge.intValue = 1;
hoge.setPrivateInt(999);
hoge.intValueIgnore = 9999;
hoge.intObj = 2;
hoge.floatValue = 3.1f;
hoge.doubleValue = 4.1;
hoge.booleanValue = true;
hoge.stringValue = "abc";
hoge.intArray = new int[] {10, 20, 30};
hoge.intObjArray = new Integer[] {11, 21, null};
hoge.hogeArray = new Hoge[2];
hoge.hogeArray[0] = new Hoge();
hoge.hogeList = new ArrayList<>();
hoge.hogeList.add(new Hoge());
hoge.hogeList.add(new Hoge());
System.out.println("json.." + ClassMapper.toJson(hoge));
çµæã¯ãããªãã¾ããfloatã«èª¤å·®ãåºãã®ã¯ãã¾ããããããªã*1ã
json..{
"intValue":1,
"intObj":2,
"floatValue":3.0999999046325684,
"doubleValue":4.1,
"booleanValue":true,
"stringValue":"abc",
"intArray":[
10,
20,
30
],
"intObjArray":[
11,
21,
null
],
"hogeArray":[
{
"intValue":0,
"intObj":null,
"floatValue":0.0,
"doubleValue":0.0,
"booleanValue":false,
"stringValue":null,
"intArray":null,
"intObjArray":null,
"hogeArray":null,
"hogeList":null
},
null
],
"hogeList":[
{
"intValue":0,
"intObj":null,
"floatValue":0.0,
"doubleValue":0.0,
"booleanValue":false,
"stringValue":null,
"intArray":null,
"intObjArray":null,
"hogeArray":null,
"hogeList":null
},
{
"intValue":0,
"intObj":null,
"floatValue":0.0,
"doubleValue":0.0,
"booleanValue":false,
"stringValue":null,
"intArray":null,
"intObjArray":null,
"hogeArray":null,
"hogeList":null
}
]
}
éã«ãã®JSONãHogeã¯ã©ã¹ã«å¤æãã§ããã®ã§ãããå¶éã¨ãã¦ãJSONããã¯ã©ã¹ã«å¤æããã»ãã§ã¯Listã¯ä½¿ãã¾ãããJavaã§ã¯Genericã³ã¬ã¯ã·ã§ã³ã¯ã©ã¹ã使ã£ã¦ãå®è¡æã«ã¯è¦ç´ ã®ã¯ã©ã¹ãç¹å®ã§ããªãããã§ããã ããJavaã®Genericsã£ã¦ã®ã¯â¦â¦(以ä¸èªç²)
ä¸ã®JSONãããhogeListã«ç¸å½ããé¨åãåé¤ããã°ã以ä¸ã®ããã«ClassMapper.toObject()ã§JSONããã¯ã©ã¹ã®ãªãã¸ã§ã¯ãã«å¤æã§ãã¾ããClassMapper.toObject()ã®ç¬¬ï¼å¼æ°ã¯ãå¤æ対象ã®ã¯ã©ã¹ã®Classã§ãã
String jsonStr = """
{
"intValue":1,
"intObj":2,
"floatValue":3.0999999046325684,
"doubleValue":4.1,
"booleanValue":true,
"stringValue":"abc",
"intArray":[
10,
20,
30
],
"intObjArray":[
11,
21,
null
],
"hogeArray":[
{
"intValue":0,
"intObj":null,
"floatValue":0.0,
"doubleValue":0.0,
"booleanValue":false,
"stringValue":null,
"intArray":null,
"intObjArray":null,
"hogeArray":null,
"hogeList":null
},
null
]
}
""";
Hoge hoge = ClassMapper.toObject(jsonStr, Hoge.class);
JSONã§ã¯ãããã ã®intã®æåå("5"ã¨ã)ããç«æ´¾ãªJSONãªã®ã§ããã®ãããã®å¤æã«ã対å¿ãã¦ãã¾ãã
Integer int1 = ClassMapper.toObject("5", int.class);
assertEquals(5, int1.intValue());
ã©ã¤ã»ã³ã¹ã¯ç§ã®ç¥ãéããã£ã¨ãç·©ãã©ã¤ã»ã³ã¹ã§ããNYSL(ç
®ããªãç¼ããªã好ãã«ããã©ã¤ã»ã³ã¹)ãªã®ã§ããããã°ä½¿ã£ã¦ãã£ã¦ãã ããã¾ãã