Built-in JSON & XML
Parse, generate, and manipulate JSON and XML with Oorian's native support.
No External Dependencies
Oorian includes its own JSON and XML utilities. No need for Jackson, Gson, or other libraries.
JSON Objects
JsonObject class for key-value data. Easy creation, access, and modification.
JSON Arrays
JsonArray class for ordered collections. Iterate, add, remove with familiar methods.
XML Support
Full XML parsing and generation. Create, traverse, and modify XML documents.
Parsing
Parse JSON strings and XML files into Java objects. Handle any structure.
Generation
Generate JSON and XML from Java objects. Automatic formatting.
Jsonable Interface
Implement Jsonable to make your classes automatically serializable to JSON.
JSON API
Create and manipulate JSON with simple, intuitive methods.
// Create a JSON object
JsonObject user = new JsonObject();
user.put("name", "John Doe");
user.put("email", "john@example.com");
user.put("age", 30);
user.put("active", true);
// Create nested objects
JsonObject address = new JsonObject();
address.put("street", "123 Main St");
address.put("city", "Springfield");
user.put("address", address);
// Create arrays
JsonArray roles = new JsonArray();
roles.add("admin");
roles.add("editor");
user.put("roles", roles);
// Access values
String name = user.getAsString("name");
int age = user.getAsInt("age");
boolean active = user.getAsBoolean("active");
JsonObject addr = user.getAsJsonObject("address");
// Parse from string
String jsonString = "{\"name\":\"Jane\",\"age\":25}";
JsonObject parsed = JsonObject.parse(jsonString);
// Convert to string
String output = user.toString();XML API
Parse, create, and modify XML documents with Oorian's XML utilities.
// Parse XML from string
String xml = "<users><user name='John'/><user name='Jane'/></users>";
XmlDocument doc = XmlParser.parse(xml);
// Navigate the document
XmlElement root = doc.getRoot();
List<XmlElement> users = root.getChildrenByName("user");
for (XmlElement user : users)
{
String name = user.getAttributes().get("name");
System.out.println("User: " + name);
}
// Create XML programmatically
XmlElement config = new XmlElement("config");
XmlElement database = new XmlElement("database");
database.addAttribute("type", "postgresql");
config.addChild(database);
XmlElement host = new XmlElement("host").setText("localhost");
XmlElement port = new XmlElement("port").setText("5432");
XmlElement dbName = new XmlElement("name").setText("myapp");
database.addChild(host).addChild(port).addChild(dbName);
XmlDocument newDoc = new XmlDocument(config);
// Convert to string
String xmlOutput = newDoc.toString();The Jsonable Interface
Implement Jsonable to make your model classes automatically serializable to JSON.
public class Product implements Jsonable
{
private String id;
private String name;
private double price;
private boolean inStock;
// Getters and setters...
@Override
public void initFromJson(JsonValue json)
{
JsonObject obj = json.asObject();
this.id = obj.getAsString("id");
this.name = obj.getAsString("name");
this.price = obj.getAsDouble("price");
this.inStock = obj.getAsBoolean("inStock", false);
}
@Override
public JsonValue toJsonValue()
{
JsonObject json = new JsonObject();
json.put("id", id);
json.put("name", name);
json.put("price", price);
json.put("inStock", inStock);
return json;
}
}
// Usage
Product product = new Product();
product.setId("SKU-001");
product.setName("Widget");
product.setPrice(29.99);
product.setInStock(true);
// Convert to JSON
JsonValue json = product.toJsonValue();
String jsonString = json.toString();
// JsonObject.put() accepts Jsonable directly
JsonObject order = new JsonObject();
order.put("product", product); // Automatically calls toJsonValue()