public class Odin
extends java.lang.Object
OdinExtra
, so all OdinElement
can be write with multiple comments or empty lines. {
node = {
a bool = true
a int = 10
}
string = "
Multilines string
With a single line separator
"
}
- Compressed, the default format, used for network or data storage, ',' is used as entry separator and no more space or tabulation is added, all line separator in String are escaped. {node={a bool=true,a int=10},string="Multiline string\nWith a single line separator"}
OdinElement.getExtra()
but also any type of java object.OdinArray
and java array object, use '[' and ']' as delimiter. the type definition is write with an additional '[' before name for each dimension. <[[int>[[1,2],[3,4]]
- Object, used by OdinNode
and all type which are not listed above, use '{' and '}' as delimiter.OdinNode
and default object adapter. Field name use escape for all special char like
tabulation or line separator, space and tabulation before and after the key was ignored on read :
{key 1=value 1,key 2=value 2}
- Value, used by List
and Set
adapter, value is write without key :
{value 1,value 2}
- Row, used by Map
adapter, row is a element with a list of value, each value is associated together and separated by a ':', a row can contains an unlimited number of
values :
{field 1=field value,field 2=field value}
The root writer and reader is consider as Object and can contains directly these 3 types of elements {content={id=1,parent=(1)}}
The int is the position of the object from the current object, 0 is herself, 1 is parent, ...register(OdinAdapter)
or register(String, OdinAdapter)
to also add a type alias.OdinObjectAdapter
: the standard adapter, used to process default object adapterOdinImmutableAdapter
: used to read immutable object needing to get content passed to constructor.OdinInlineAdapter
: used to map an object type to an another, like class, enum and date.OdinObjectAdapter
is used as runtime adapter, the object itself is used as the adapter, and write and read method is called inside the object instance.OdinField
, used to rename a field or add comments lines before it on writer.OdinMapAsList
, used to write map as a list by searching key inside value fields.OdinCustomAdapter
, used to change a field adapter without registering a global adapter in Odin.Constructor and Description |
---|
Odin() |
Modifier and Type | Method and Description |
---|---|
Odin |
compressOutput()
Change output to be short as possible, ',' is used as entry separator and no new line or indentation is used.
Example with a simple node write to file : |
<T> T |
fromOdn(java.lang.String odn)
Transform an odn string to object using
OdinReader.read() |
<T> T |
fromOdn(java.lang.String odn,
java.lang.reflect.Type type)
Transform an odn string to object using
OdinReader.readTyped(Type) |
<T> OdinAdapter<T> |
getAdapter(java.lang.reflect.Type type)
Get the registered adapter for given type, this method retrieve nearest adapter in parents class if the type has no registered adapter
|
DefaultAdapterOption |
getDefaultAdapterOption()
Change the default object adapter options.
This options allow to change the way to write and read all object without custom adapters for some special case : - what to do with reflect exception - what to do with reader exception - what to do with writer exception - what to do with null fields value |
int |
getReaderBufferSize()
Get the actual buffer size used in
OdinReader . |
<T> AnyType<T> |
getType(java.lang.String name)
Get the odin type associated to a classname or type alias, if no odin type is associated to this type, odin will create it using
Class.forName(String) . |
<T> AnyType<T> |
getType(java.lang.reflect.Type type)
Get the odin type associated to a type, if no odin type is associated to this type, odin will create it.
Class always use the same odin type instance for same Odin instance, except generic types like parameterized type or generic array type, a new odin instance is
created for each call to this method. |
Odin |
indentOutput()
Change output to be indented and human readable.
|
boolean |
isIndented()
Get if writer use an indented output.
|
<T> Odin |
name(java.lang.String name,
java.lang.Class<T> type)
Add an alias for a class, an alias replace the class name by name in stream.
Example: <com.newpixelcoffee.odin.Odin> by <odin> |
OdinReader |
reader(java.io.File file)
Create a new
OdinReader using io File , the given file is transformed to reader with UTF-8 charset |
OdinReader |
reader(java.io.InputStream input)
Create a new
OdinReader using io InputStream , the given input stream is transformed to reader with UTF-8 charset |
OdinReader |
reader(java.nio.file.Path path)
Create a new
OdinReader using nio2 Path , the given path is transformed to reader with UTF-8 charset. |
OdinReader |
reader(java.io.Reader reader)
Create a new
OdinReader using io Reader |
<T> Odin |
register(OdinAdapter<T> adapter)
Register an adapter in order to modify the writer and reader behavior, an adapter can be defined for any object except primitives and arrays.
Three type of adapter is available : - OdinObjectAdapter is the standard adapter, in write and read process you receive the object type and object instance, you say to odin what to do with that, for example all object use
by default an OdinObjectAdapter, for each fields this adapter call writeTyped and readTyped.- OdinImmutableAdapter is used to read immutable object needing to get content passed to constructor, this adapter does not have recursivity check.- OdinInlineAdapter is used to transform an object to another object like primitive, for example this adapter is used to transform Class, Enum, UUID or Date to a simple String, this
adapter does not have recursive check. |
<T> Odin |
register(java.lang.String name,
OdinAdapter<T> adapter)
Register an adapter and a alias in order to modify the writer and reader behavior, an adapter can be defined for any object except primitives and arrays.
Three type of adapter is available : - OdinObjectAdapter is the standard adapter, in write and read process you receive the object type and object instance, you say to odin what to do with that, for example all object use
by default an OdinObjectAdapter, for each fields this adapter call writeTyped and readTyped.- OdinImmutableAdapter is used to read immutable object who need content value to be passed in constructor, this adapter does not have recursivity check.- OdinInlineAdapter is used to transform an object to another object like primitive, for example this adapter is used to transform Class, Enum, UUID or Date to a simple String. |
<T extends java.util.List> |
registerAsList(java.lang.String name,
java.lang.Class<T> type)
Register a class in order to process it as a list, by default only ArrayList, LinkedList and Vector use the list adapter.
|
<T extends java.util.Map> |
registerAsMap(java.lang.String name,
java.lang.Class<T> type)
Register a class in order to process it as a map, by default only HashMap, LinkedHashMap, TreeMap and EnumMap use the map adapter.
|
<T extends java.util.Set> |
registerAsSet(java.lang.String name,
java.lang.Class<T> type)
Register a class in order to process it as a set, by default only HashSet, LinkedHashSet, TreeSet and EnumSet use the set adapter.
|
void |
setDefaultDateFormat(java.lang.Class<? extends java.time.temporal.Temporal> dateTimeType,
java.time.format.DateTimeFormatter format)
Change the default local date/time format located in odin adapter, list of default java time format :
- LocalDate : yyyy-MM-dd - LocalDateTime : yyyy-MM-dd HH:mm:ss [NANO_OF_SECOND] - LocalTime : HH:mm:ss [NANO_OF_SECOND] - ZonedDateTime : yyyy-MM-dd HH:mm:ss [NANO_OF_SECOND] [ZoneOrZoneID] '[]' refers to optional part |
void |
setDefaultDateFormat(java.text.DateFormat format)
Change the default date format located in odin adapter, by default, date use 'yyyy-MM-dd HH:mm:ss'.
|
Odin |
setFieldFilter(java.util.function.Predicate<java.lang.reflect.Field> filter)
Change the class fields filter to keep only wanted fields.
|
Odin |
setReaderBufferSize(int readerBufferSize)
Set buffer size for futurely created readers.
|
java.lang.String |
toOdn(java.lang.Object o)
Transform object to an odn string using
OdinWriter.write(Object) |
java.lang.String |
toOdn(java.lang.Object o,
java.lang.reflect.Type type)
Transform object to an odn string using
OdinWriter.writeTyped(Object, Type) |
OdinWriter |
writer(java.io.File file)
Create a new
OdinWriter using io File , the given file is transformed to writer with UTF-8 charset.If the file does not exist, it will be created |
OdinWriter |
writer(java.io.OutputStream output)
Create a new
OdinWriter using io OutputStream , the given output stream is transformed to writer with UTF-8 charset |
OdinWriter |
writer(java.nio.file.Path path)
Create a new
OdinWriter using nio2 Path , the given path is transformed to writer with UTF-8 charset.If the file does not exist, it will be created |
OdinWriter |
writer(java.io.Writer writer)
Create a new
OdinWriter using io Writer |
public <T> Odin name(java.lang.String name, java.lang.Class<T> type)
T
- Class typename
- aliastype
- class to nameOdinTypeException
- if type is primitive or arraypublic <T> Odin register(OdinAdapter<T> adapter)
OdinObjectAdapter
is the standard adapter, in write and read process you receive the object type and object instance, you say to odin what to do with that, for example all object use
by default an OdinObjectAdapter, for each fields this adapter call writeTyped and readTyped.OdinImmutableAdapter
is used to read immutable object needing to get content passed to constructor, this adapter does not have recursivity check.OdinInlineAdapter
is used to transform an object to another object like primitive, for example this adapter is used to transform Class, Enum, UUID or Date to a simple String, this
adapter does not have recursive check.T
- adapter object typeadapter
- adapter to registerOdinTypeException
- if adapter type is primitive or arraypublic <T> Odin register(java.lang.String name, OdinAdapter<T> adapter)
OdinObjectAdapter
is the standard adapter, in write and read process you receive the object type and object instance, you say to odin what to do with that, for example all object use
by default an OdinObjectAdapter, for each fields this adapter call writeTyped and readTyped.OdinImmutableAdapter
is used to read immutable object who need content value to be passed in constructor, this adapter does not have recursivity check.OdinInlineAdapter
is used to transform an object to another object like primitive, for example this adapter is used to transform Class, Enum, UUID or Date to a simple String.T
- adapter object typename
- adapter type aliasadapter
- adapter to registerOdinTypeException
- if adapter type is primitive or arrayname(String, Class)
,
register(OdinAdapter)
public <T extends java.util.List> Odin registerAsList(java.lang.String name, java.lang.Class<T> type)
Register a class in order to process it as a list, by default only ArrayList, LinkedList and Vector use the list adapter.
List that does not use the list adapter are serialized with the default object adapter, by serializing the list fields.
The class need to contains a valid List.add(Object)
method.
If the type of your list is immutable and need to get the content in the constructor, create a custom list adapter.
T
- class typename
- adapter type aliastype
- class to registerpublic <T extends java.util.Set> Odin registerAsSet(java.lang.String name, java.lang.Class<T> type)
Register a class in order to process it as a set, by default only HashSet, LinkedHashSet, TreeSet and EnumSet use the set adapter.
Set that does not use the set adapter are serialized with the default object adapter, by serializing the set fields.
The class need to contains a valid List.add(Object)
method.
If the type of your set is immutable and need to get the content in the constructor, create a custom set adapter.
T
- class typename
- adapter type aliastype
- class to registerpublic <T extends java.util.Map> Odin registerAsMap(java.lang.String name, java.lang.Class<T> type)
Register a class in order to process it as a map, by default only HashMap, LinkedHashMap, TreeMap and EnumMap use the map adapter.
Map that does not use the map adapter are serialized with the default object adapter, by serializing the map fields.
The class need to contains a valid List.add(Object)
method.
If the type of your map is immutable and need to get the content in the constructor, create a custom map adapter.
T
- class typename
- adapter type aliastype
- class to registerpublic <T> AnyType<T> getType(java.lang.reflect.Type type)
Odin
instance, except generic types like parameterized type or generic array type, a new odin instance is
created for each call to this method.T
- generic typetype
- type to getpublic <T> AnyType<T> getType(java.lang.String name)
Class.forName(String)
.T
- generic typename
- type to getpublic <T> OdinAdapter<T> getAdapter(java.lang.reflect.Type type)
T
- adapter typetype
- adapter typejava.lang.IllegalStateException
- if type is primitive or array, or if the type use a runtime adapterpublic Odin setFieldFilter(java.util.function.Predicate<java.lang.reflect.Field> filter)
Change the class fields filter to keep only wanted fields. This filter affect object type and not only the default object adapter.
By this way, if a field is hidden by this filter, it will not be present in the result of ObjectType.findField(String)
and other fields access methods
The default filter hide transient and static fields. You can filter in a custom annotation using AccessibleObject.isAnnotationPresent(Class)
filter
- the filter to usepublic void setDefaultDateFormat(java.text.DateFormat format)
format
- new date formatOdinTypeException
- if date does not use default date adapterpublic void setDefaultDateFormat(java.lang.Class<? extends java.time.temporal.Temporal> dateTimeType, java.time.format.DateTimeFormatter format)
dateTimeType
- local date/time typeformat
- new date formatOdinTypeException
- if date does not use default date adapterpublic DefaultAdapterOption getDefaultAdapterOption()
public OdinWriter writer(java.io.Writer writer)
OdinWriter
using io Writer
writer
- writer to usepublic OdinWriter writer(java.io.OutputStream output)
OdinWriter
using io OutputStream
, the given output stream is transformed to writer with UTF-8 charsetoutput
- stream to usepublic OdinWriter writer(java.nio.file.Path path) throws java.io.IOException
OdinWriter
using nio2 Path
, the given path is transformed to writer with UTF-8 charset.path
- path to usejava.io.IOException
- if the path generate an IO exceptionpublic OdinWriter writer(java.io.File file) throws java.io.FileNotFoundException
OdinWriter
using io File
, the given file is transformed to writer with UTF-8 charset.file
- file to usejava.io.FileNotFoundException
- if file does not exist and cannot be created or file is a directorypublic OdinReader reader(java.io.Reader reader)
OdinReader
using io Reader
reader
- reader to usejava.lang.IllegalArgumentException
- if the reader buffer size is lower than 1public OdinReader reader(java.io.InputStream input)
OdinReader
using io InputStream
, the given input stream is transformed to reader with UTF-8 charsetinput
- input to usejava.lang.IllegalArgumentException
- if the reader buffer size is lower than 1public OdinReader reader(java.nio.file.Path path) throws java.io.IOException
OdinReader
using nio2 Path
, the given path is transformed to reader with UTF-8 charset.path
- path to usejava.io.IOException
- is an IO exception occurjava.lang.IllegalArgumentException
- if the reader buffer size is lower than 1public OdinReader reader(java.io.File file) throws java.io.FileNotFoundException
OdinReader
using io File
, the given file is transformed to reader with UTF-8 charsetfile
- file to usejava.io.FileNotFoundException
- if file is not accessiblejava.lang.IllegalArgumentException
- if the reader buffer size is lower than 1public java.lang.String toOdn(java.lang.Object o)
OdinWriter.write(Object)
o
- object to writeOdinTypeException
- if the type adapter can not be build for this object.
Sub object type error is placed as cause of a OdinAdapterException
OdinAdapterException
- if a runtime exception is throw by an adapter when writing the objectpublic java.lang.String toOdn(java.lang.Object o, java.lang.reflect.Type type)
OdinWriter.writeTyped(Object, Type)
o
- object to writetype
- object generic typeOdinTypeException
- if the type adapter can not be build for this object.
Sub object type error is placed as cause of a OdinAdapterException
OdinAdapterException
- if a runtime exception is throw by an adapter when writing the objectpublic <T> T fromOdn(java.lang.String odn)
OdinReader.read()
T
- object typeodn
- String to readOdinFormatException
- if the reader reach an invalid element delimiter or wrong element formatOdinTypeException
- if the type adapter can not be build for an objectOdinAdapterException
- if a runtime exception is throw by an adapter when reading the objectpublic <T> T fromOdn(java.lang.String odn, java.lang.reflect.Type type)
OdinReader.readTyped(Type)
T
- object typeodn
- String to readtype
- object typeOdinFormatException
- if the reader reach an invalid element delimiter or wrong element formatOdinTypeException
- if the type adapter can not be build for an objectOdinAdapterException
- if a runtime exception is throw by an adapter when reading the objectpublic boolean isIndented()
indentOutput()
or compressOutput()
can be different from this valuepublic Odin indentOutput()
{
node = {
a bool = true
a int = 10
}
a char = 'c'
}
String containing line return is write as multilines String :
string = "
This is a multiline string
With a single line separator
"
Multilines string use the line separator in the String and not the Writer line separator, so a String with a \n at end of first line and \r in second line is write with only this \n or \n at
end of lines in order to keep the good chars when reading the String. string = "
This is a multiline string
\ With a single line separator
"
Indented output allowing the use of OdinExtra
, so all OdinElement
can be write with multiple comments or empty lines :
{
node = {
# COMMENT 1
# COMMENT 2
# COMMENT 3
a bool = true
# COMMENT 4
}
}
compressOutput()
public Odin compressOutput()
{node={a bool=true,a int=10},a char='c'}
String containing line return is write with backslash :
string="This is a multiline string\nWith a single line separator"
indentOutput()
public int getReaderBufferSize()
OdinReader
. reader created before call to setReaderBufferSize(int)
can have a different buffer size. The default buffer size is 4096public Odin setReaderBufferSize(int readerBufferSize)
readerBufferSize
- new buffer size