Seven runnable programs P01-P07 plus five compile-failure files, JDK 25, no docs/ folder. Output in objectmapper-jsonmapper/output/ is regenerated by scripts/run-all.sh. Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b75Ao6imWN5bj7FHVw
104 lines
5.7 KiB
Java
104 lines
5.7 KiB
Java
package com.ankurm.objectmapper;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.StringReader;
|
|
import java.lang.reflect.Method;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
import tools.jackson.core.type.TypeReference;
|
|
import tools.jackson.databind.JsonNode;
|
|
import tools.jackson.databind.ObjectReader;
|
|
import tools.jackson.databind.json.JsonMapper;
|
|
|
|
/** Every way in and out, on Jackson 3. One mapper, built once. */
|
|
public class P03ReadWriteCookbook {
|
|
public static void main(String[] args) throws Exception {
|
|
JsonMapper mapper = JsonMapper.builder().build();
|
|
Article article = new Article(1L, "Jackson Deep Dive", List.of("java", "json"));
|
|
|
|
// ---- WRITE ----
|
|
String asString = mapper.writeValueAsString(article);
|
|
byte[] asBytes = mapper.writeValueAsBytes(article);
|
|
Path file = Files.createTempFile("article", ".json");
|
|
mapper.writeValue(file, article); // Path (java.nio)
|
|
mapper.writeValue(file.toFile(), article); // File
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
mapper.writeValue(out, article); // OutputStream
|
|
System.out.println("writeValueAsString : " + asString);
|
|
System.out.println("writeValueAsBytes : " + asBytes.length + " bytes");
|
|
System.out.println("writeValue(Path) : " + Files.readString(file));
|
|
System.out.println("writeValue(Stream) : " + out.size() + " bytes");
|
|
System.out.println();
|
|
|
|
// ---- READ ----
|
|
System.out.println("read String : " + mapper.readValue(asString, Article.class).getTitle());
|
|
System.out.println("read byte[] : " + mapper.readValue(asBytes, Article.class).getTitle());
|
|
System.out.println("read File : " + mapper.readValue(file.toFile(), Article.class).getTitle());
|
|
System.out.println("read Path : " + mapper.readValue(file, Article.class).getTitle());
|
|
System.out.println("read InputStream : "
|
|
+ mapper.readValue(new ByteArrayInputStream(asBytes), Article.class).getTitle());
|
|
System.out.println("read Reader : "
|
|
+ mapper.readValue(new StringReader(asString), Article.class).getTitle());
|
|
Files.delete(file);
|
|
System.out.println();
|
|
|
|
// ---- GENERIC TYPES: a Class<T> cannot carry List<Article>, a TypeReference can ----
|
|
String array = "[{\"articleId\":1,\"title\":\"First\"},{\"articleId\":2,\"title\":\"Second\"}]";
|
|
List<Article> list = mapper.readValue(array, new TypeReference<List<Article>>() { });
|
|
System.out.println("List<Article> size : " + list.size() + ", element class: " + list.get(0).getClass().getSimpleName());
|
|
List<?> raw = mapper.readValue(array, List.class);
|
|
System.out.println("List.class element : " + raw.get(0).getClass().getName());
|
|
System.out.println();
|
|
|
|
// ---- TREE and convertValue ----
|
|
JsonNode tree = mapper.readTree(asString);
|
|
System.out.println("tree title : " + tree.get("title").asString());
|
|
System.out.println("tree tags[1] : " + tree.get("tags").get(1).asString());
|
|
Map<String, Object> asMap = mapper.convertValue(article, new TypeReference<Map<String, Object>>() { });
|
|
System.out.println("convertValue to Map : " + asMap);
|
|
Article fromMap = mapper.convertValue(Map.of("articleId", 9, "title", "From a Map"), Article.class);
|
|
System.out.println("convertValue to bean : " + fromMap.getArticleId() + " / " + fromMap.getTitle());
|
|
System.out.println();
|
|
|
|
// ---- Reusable reader / writer: cheap, thread-safe, fixed to one type ----
|
|
ObjectReader articleReader = mapper.readerFor(Article.class);
|
|
Article viaReader = articleReader.readValue(asString);
|
|
System.out.println("readerFor(Article) : " + viaReader.getTitle());
|
|
System.out.println("pretty writer : "
|
|
+ mapper.writerWithDefaultPrettyPrinter().writeValueAsString(Map.of("k", 1)).replace("\n", "\\n"));
|
|
System.out.println();
|
|
|
|
// ---- Is there a readValue(URL) in each major? ----
|
|
System.out.println("Jackson 2 readValue(URL, Class) exists : " + hasUrlOverload(
|
|
com.fasterxml.jackson.databind.ObjectMapper.class));
|
|
System.out.println("Jackson 3 readValue(URL, Class) exists : " + hasUrlOverload(
|
|
tools.jackson.databind.ObjectMapper.class));
|
|
System.out.println("Jackson 3 readTree(URL) exists : " + Arrays.stream(
|
|
tools.jackson.databind.ObjectMapper.class.getMethods())
|
|
.filter(m -> m.getName().equals("readTree"))
|
|
.anyMatch(m -> m.getParameterCount() == 1 && m.getParameterTypes()[0] == java.net.URL.class));
|
|
String marathi = "\u092e\u0930\u093e\u0920\u0940"; // five Devanagari code points
|
|
byte[] utf8 = mapper.writeValueAsBytes(new Article(2L, marathi, null));
|
|
System.out.println("non-ASCII byte[] round trip equal : "
|
|
+ mapper.readValue(utf8, Article.class).getTitle().equals(marathi)
|
|
+ " (bytes are " + StandardCharsets.UTF_8.name() + " by default: "
|
|
+ new String(utf8, StandardCharsets.UTF_8).contains(marathi) + ")");
|
|
}
|
|
|
|
private static boolean hasUrlOverload(Class<?> mapperClass) {
|
|
for (Method m : mapperClass.getMethods()) {
|
|
if (m.getName().equals("readValue") && m.getParameterCount() == 2
|
|
&& m.getParameterTypes()[0] == java.net.URL.class) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}
|