Skip to content
stemey edited this page Oct 21, 2012 · 11 revisions

The json repository uses the jackson library for mapping java objects to json. The mapping part itself is not used but the Java representation of json objects. The repository is a DynamicEntityTypeSubrepository. The EntityTypes handle instances of ObjectNode, which is the Jackson representation of a json object.

the basic configuration needs to provide an instance of ObjectMapper (see also json-example]):

<beans>
	<import resource="classpath:/meta/jackson/entitytype.xml" />

	<bean id="objectMapper"
		class="org.codehaus.jackson.map.ObjectMapper">
	</bean>

	<bean id="json-repository"
		class="de.sinnerschrader.fue.meta.impl.json.JsonEntityTypeRepository">
		<property name="objectMapper ref="objectMapper"/>	
		<property name="typeProperty" value="ext_type" />
	</bean>
</beans>

The json repository depends on an instanceof ObjectMapper. The ObjectMapper defines how json is parsed and written.

To map a json object to an atem type the json object needs to provide the type code in a special property. The name of the type property defauts to "ext_type" but can be configured for the repository.

The atem type code must be unique in the atem repository. In the case of versioned types there can be multiple types for the model "Account" - "Account:1.0", "Account:2.0" and so on. The json object should usually have use only Account as the value of the type property because the client onlydealswith one of the versions. Therefore it is possible to define a TypeCodeConverter to convert from the internal type code to the external and back.

generic json

the class ObjectNode from the jackson library represents json nodes. There is an EntityType to operate on ObjectNode's without a specific type. This type has one property called properties, which is a MapAttribute without targetType. The other EntityType is the type for arrayNode representing an array. The only attribute in ArrayNode is "array" which is a CollectionAttribute without targetType.

{
name:"Willi",
addresses:[
	{street:"Sesamestreet"},
	{street:"Sesamestreet"},
]

Accessing the ObjectNode refelectively :

EntityType jsonType=EntityTypeRepository.geEntityType(ObjectNode.class");
MapAttribute properties = (MapAttribute) jsonType.getAttribute("properties");
String name = properties.getElement(objectNode,"name");

Working on the array:

EntityType arrayType=EntityTypeRepository.geEntityType(ArrayNode.class");
ArrayNode addresses = properties.getElement(objectNode,"addresses");
ListAttribute addressList = (ListAttribute) arrayType.getAttribute(addresses,"array");
ObjectNode secondAddress = addressList.getElement(addresses,2);

To compare two json strings you can for example do this:

ObjectNode node2=objectMapper.readTree("{name:'Willi'}");
ObjectNode node1=objectMapper.readTree("{name:'Hendrik'}");

// jsonComparator is a standard comparison instance created for plain json.
List<Difference> differences  = jsonComparator.geDifferences(node1,node2);  
Assert.assertTrue("properties.name",differences.get(0).getPath());