Skip to content

Instantly share code, notes, and snippets.

@tag1216
Created March 1, 2015 22:36
Show Gist options
  • Save tag1216/4ef77ee38533b8bd664c to your computer and use it in GitHub Desktop.
Save tag1216/4ef77ee38533b8bd664c to your computer and use it in GitHub Desktop.
GSONのJsonObjectでJsonを構築する時はビルダーを作っておくと美しいコードになる ref: http://qiita.com/tag1216/items/12d92662fd1ea25329c9
@AllArgsConstructor
public class Item {
public String id;
public String title;
public User user;
public List<Tag> tags;
}
JsonArray itemArray =
Json.toJsonArray2(items, new ToJson<Item>() {
@Override
public JsonElement apply(Item item) {
return Json.createObjectBuilder()
.add("id", item.id)
.add("title", item.title)
.add("user", Json.createObjectBuilder()
.add("id", item.user.id)
.add("name", item.user.name)
.build()
)
.add("tags", Json.toJsonArray2(item.tags, new ToJson<Tag>() {
@Override
public JsonElement apply(Tag tag) {
return Json.createObjectBuilder()
.add("url", tag.url)
.build();
}
}))
.build();
}
});
JsonArray array = new JsonArray();
for (Item item : items) {
JsonObject itemJson = new JsonObject();
itemJson.addProperty("id", item.id);
itemJson.addProperty("title", item.title);
JsonObject userJson = new JsonObject();
userJson.addProperty("id", item.user.id);
userJson.addProperty("name", item.user.name);
itemJson.add("user", userJson);
JsonArray tagArray = new JsonArray();
for (Tag tag : item.tags) {
JsonObject tagJson = new JsonObject();
tagJson.addProperty("url", tag.url);
tagArray.add(tagJson);
}
itemJson.add("tags", tagArray);
array.add(itemJson);
}
Collector<JsonElement, JsonArray, JsonArray> toJsonArray = Collector.of(
JsonArray::new,
JsonArray::add,
(a, b) -> { a.addAll(b); return a; }
);
JsonArray array = items.stream()
.map(item -> {
JsonObject itemJson = new JsonObject();
itemJson.addProperty("id", item.id);
itemJson.addProperty("title", item.title);
JsonObject userJson = new JsonObject();
userJson.addProperty("id", item.user.id);
userJson.addProperty("name", item.user.name);
itemJson.add("user", userJson);
JsonArray tagArray = item.tags.stream()
.map(tag -> {
JsonObject tagJson = new JsonObject();
tagJson.addProperty("url", tag.url);
return tagJson;
})
.collect(toJsonArray);
itemJson.add("tags", tagArray);
return itemJson;
})
.collect(toJsonArray);
return array;
JsonArray itemArray =
Json.toJsonArray(items, item -> Json.createObjectBuilder()
.add("id", item.id)
.add("title", item.title)
.add("user", Json.createObjectBuilder()
.add("id", item.user.id)
.add("name", item.user.name)
.build()
)
.add("tags", Json.toJsonArray(item.tags, tag -> Json.createObjectBuilder()
.add("url", tag.url)
.build()
))
.build()
);
public static <T> JsonArray toJsonArray2(Collection<T> list, ToJson<T> toJson) {
JsonArrayBuilder array = Json.createArrayBuilder();
for (T e : list) {
array.add(toJson.apply(e));
}
return array.build();
}
public class JsonArrayBuilder {
private JsonArray jsonArray = new JsonArray();
public JsonArray build() {
return jsonArray;
}
public JsonArrayBuilder add(JsonElement json) {
jsonArray.add(json);
return this;
}
public JsonArrayBuilder addAll(JsonArrayBuilder builder) {
jsonArray.addAll(builder.build());
return this;
}
public JsonArrayBuilder add(String value) {
jsonArray.add(new JsonPrimitive(value));
return this;
}
}
public class JsonObjectBuilder {
private JsonObject jsonObject = new JsonObject();
public JsonObject build() {
return jsonObject;
}
public JsonObjectBuilder add(String name, JsonElement json) {
jsonObject.add(name, json);
return this;
}
public JsonObjectBuilder add(String name, String value) {
jsonObject.addProperty(name, value);
return this;
}
}
@AllArgsConstructor
public class Tag {
public String url;
}
public interface ToJson<T> {
public JsonElement apply(T src);
}
@AllArgsConstructor
public class User {
public String id;
public String name;
}
List<Item> items = Arrays.asList(
new Item("item1", "title1",
new User("user1", "user1"),
Arrays.asList(new Tag("tag1"), new Tag("tag2"))),
new Item("item2", "title2",
new User("user2", "user2"),
Arrays.asList(new Tag("tag2"), new Tag("tag3")))
);
[
{
"id": "item1",
"title": "title1",
"user": {
"id": "user1",
"name": "user1"
},
"tags": [
{"url": "tag1"},
{"url": "tag2"}
]
},
{
"id": "item2",
"title": "title2",
"user": {
"id": "user2",
"name": "user2"
},
"tags": [
{"url": "tag2"},
{"url": "tag3"}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment