|
||||||||
|
Create a SiDOL schema
Create a SiDOL schema in a file myschema.sidol. For example,
schema a Person type has a firstName, a lastName, a nullable integer as age, an array of string as emails . Loads the factory and the schemaCreates a factory and loads the previously created schema:
...
DataFactory factory = DataFactories.typedFactory();
new SiDOLDataFactoryLoader().load(getClass().getResourceAsInputStream("/path/to/myschema.sidol", factory);
...
Creates an instance and plays with it
...
DataType type = factory.getType("MySchema::Person");
DataObject person = type.newInstance();
person.setString("firstName", "Damien");
person.setString("lastName", "Coraboeuf");
person.setInteger("age", 38);
person.setArray("emails", new Array
You can also chain the calls:
...
DataType type = ;
DataObject person = factory.getType("MySchema::Person")
.newInstance()
.setString("firstName", "Damien")
.setString("lastName", "Coraboeuf")
.setInteger("age", 38)
.setArray("emails", new Array
To read the values:
...
String value = person.getString("lastName");
int age = person.getInteger("age");
Array
Reading or setting non existing properties will raise an exception:
...
person.setDecimal("wages", BigDecimal.ZERO); // This raises a DOUndefinedPropertyException
...
Writing/reading SiDO objectsYou can write a DataObject to XML: ... String xml = DataObjectIO.writeString(new XMLDataObjectWriter(), person); ... or to JSON: ... String json = DataObjectIO.writeString(new JSONDataObjectWriter(), person); ... You can read those items back from XML: ... DataObject o = DataObjectIO.readString(new XMLDataObjectReader(), xml); ... or from JSON: ... DataObject o = DataObjectIO.readString(new JSONDataObjectReader(), json); ... XML/JSONDataObjectReader/Writer classes have also methods that allow to write/read at a lower level (to/from streams, to/from JSON/XML trees). CopiesAny DataObject can be copied (deep copy): DataObject copy = person.copy(); or made immutable:
DataObject copy = person.immutable();
copy.setString("firstName", "Albert"); // Will raise a DOImmutableException
|
|||||||