|
| 1 | +--- |
| 2 | +layout: module |
| 3 | +format: article |
| 4 | +title: JSON Serializer |
| 5 | +subheadline: Aspectran Modules - Utility |
| 6 | +teaser: | |
| 7 | + Java Object를 JSON(JavaScript Object Notation) 형식의 문자열로 변환하는 클래스입니다. |
| 8 | + 별도의 JSON 라이브러리를 필요로 하지 않고, 상당히 빠른 속도로 변환을 해 줍니다. |
| 9 | +category: Utility |
| 10 | +download: |
| 11 | + source: https://github.com/aspectran/aspectran/tree/master/src/main/java/com/aspectran/core/util/json/JsonSerializer.java |
| 12 | +--- |
| 13 | + |
| 14 | +## Java Source |
| 15 | + |
| 16 | +{% highlight java %} |
| 17 | +package com.aspectran.core.util.json; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | + |
| 24 | +import com.aspectran.core.util.apon.Customer; |
| 25 | + |
| 26 | +public class JsonSerializerTest { |
| 27 | + |
| 28 | + public static void main(String argv[]) { |
| 29 | + try { |
| 30 | + Map<String, Object> map = new LinkedHashMap<String, Object>(); |
| 31 | + map.put("message", "Start Testing Now!"); |
| 32 | + map.put("one", 1); |
| 33 | + map.put("two", 2); |
| 34 | + map.put("three", 3); |
| 35 | + map.put("four", 4); |
| 36 | + |
| 37 | + List<Customer> customerList = new ArrayList<Customer>(); |
| 38 | + |
| 39 | + for(int i = 1; i <= 10; i++) { |
| 40 | + Customer customer = new Customer(); |
| 41 | + customer.putValue(Customer.id, "guest-" + i); |
| 42 | + customer.putValue(Customer.name, "Guest" + i); |
| 43 | + customer.putValue(Customer.age, 20 + i); |
| 44 | + customer.putValue(Customer.episode, "His individual skills are outstanding.\nI don't know as how he is handsome."); |
| 45 | + customer.putValue(Customer.approved, true); |
| 46 | + |
| 47 | + customerList.add(customer); |
| 48 | + } |
| 49 | + |
| 50 | + map.put("customers", customerList); |
| 51 | + |
| 52 | + System.out.println(JsonSerializer.serialize(map, true, " ")); |
| 53 | + |
| 54 | + } catch(Exception e) { |
| 55 | + e.printStackTrace(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | +} |
| 60 | +{% endhighlight %} |
| 61 | + |
| 62 | +## 출력 결과 |
| 63 | + |
| 64 | +{% highlight text %} |
| 65 | +{ |
| 66 | + "message": "Start Testing Now!", |
| 67 | + "one": 1, |
| 68 | + "two": 2, |
| 69 | + "three": 3, |
| 70 | + "four": 4, |
| 71 | + "customers": [ |
| 72 | + { |
| 73 | + "id": "guest-1", |
| 74 | + "name": "Guest1", |
| 75 | + "age": 21, |
| 76 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 77 | + "approved": "true" |
| 78 | + }, |
| 79 | + { |
| 80 | + "id": "guest-2", |
| 81 | + "name": "Guest2", |
| 82 | + "age": 22, |
| 83 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 84 | + "approved": "true" |
| 85 | + }, |
| 86 | + { |
| 87 | + "id": "guest-3", |
| 88 | + "name": "Guest3", |
| 89 | + "age": 23, |
| 90 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 91 | + "approved": "true" |
| 92 | + }, |
| 93 | + { |
| 94 | + "id": "guest-4", |
| 95 | + "name": "Guest4", |
| 96 | + "age": 24, |
| 97 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 98 | + "approved": "true" |
| 99 | + }, |
| 100 | + { |
| 101 | + "id": "guest-5", |
| 102 | + "name": "Guest5", |
| 103 | + "age": 25, |
| 104 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 105 | + "approved": "true" |
| 106 | + }, |
| 107 | + { |
| 108 | + "id": "guest-6", |
| 109 | + "name": "Guest6", |
| 110 | + "age": 26, |
| 111 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 112 | + "approved": "true" |
| 113 | + }, |
| 114 | + { |
| 115 | + "id": "guest-7", |
| 116 | + "name": "Guest7", |
| 117 | + "age": 27, |
| 118 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 119 | + "approved": "true" |
| 120 | + }, |
| 121 | + { |
| 122 | + "id": "guest-8", |
| 123 | + "name": "Guest8", |
| 124 | + "age": 28, |
| 125 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 126 | + "approved": "true" |
| 127 | + }, |
| 128 | + { |
| 129 | + "id": "guest-9", |
| 130 | + "name": "Guest9", |
| 131 | + "age": 29, |
| 132 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 133 | + "approved": "true" |
| 134 | + }, |
| 135 | + { |
| 136 | + "id": "guest-10", |
| 137 | + "name": "Guest10", |
| 138 | + "age": 30, |
| 139 | + "epsode": "His individual skills are outstanding.\nI don't know as how he is handsome.", |
| 140 | + "approved": "true" |
| 141 | + } |
| 142 | + ] |
| 143 | +} |
| 144 | +{% endhighlight %} |
0 commit comments