Converter
Overview
Module
- package:
org.eclipse.dirigible.sdk.utils - source: utils/Converter.java
Quick JSON serialization helpers around Jackson's default ObjectMapper. Convenient when you just need String / object conversion without configuring a mapper instance — for non-trivial schemas (custom serializers, mixed polymorphism, date format overrides) inject a Jackson ObjectMapper bean instead.
XML conversion lives in Xml; CSV parsing requires a dedicated library (Apache Commons CSV or OpenCSV) — this class deliberately does not pretend to handle quoting and escaping around the edges of CSV semantics.
Key Features:
- Default Jackson
ObjectMapper: Shared static instance — zero configuration on the call site. - Typed and untyped parsing:
fromJson(String, Class)for typed bindings,fromJson(String)forMap/Listshapes. - Wrapping exception: Jackson's checked
JsonProcessingExceptionis wrapped asIllegalArgumentExceptionfor ergonomic call sites.
Example Usage:
import org.eclipse.dirigible.sdk.utils.Converter;
import java.util.Map;
// Serialize to JSON
String json = Converter.toJson(Map.of("name", "Eclipse Dirigible", "version", 13));
// → {"name":"Eclipse Dirigible","version":13}
// Untyped parse — comes back as Map / List / primitives
Object generic = Converter.fromJson(json);
// Typed parse
record Project(String name, int version) {}
Project project = Converter.fromJson(json, Project.class);Methods
toJson()
Serializes the supplied object to a JSON string using the default ObjectMapper.
javapublic static String toJson(Object input);
Parameter Type Description inputObjectThe value to serialize. May be a POJO, Map,List, primitive, or array.Returns
- Type:
String- Description: The JSON-serialized form of
input. ThrowsIllegalArgumentException(wrapping Jackson'sJsonProcessingException) if serialization fails.
fromJson()
Parses a JSON string. The single-argument overload returns a generic Object graph (Map/List/primitives); the two-argument overload binds the JSON to the supplied type.
javapublic static <T> T fromJson(String input, Class<T> type); public static Object fromJson(String input);
Parameter Type Description inputStringThe JSON text to parse. typeClass<T>Target type to bind to (only on the typed overload). Returns
- Type:
T(typed overload) orObject(untyped overload)- Description: The deserialized value. Throws
IllegalArgumentException(wrapping Jackson'sJsonProcessingException) if parsing fails.