How to transform a LinkedHashMap Payload to Object Payload in mule? - object

I want to transform a LinkedHashMap Payload to Object Payload in mule , i used the Byte Array to Object transformer but it dosent work for me , any idea guys ?

you can use dataweave to transform a payload of generic type (=java.util.Map) to a specific type (foo.bar.Type in the example):
%dw 1.0
%output application/java
---
payload as :object {
class: "foo.bar.Type"
}

You seem to mention specifically the Object type. A LinkedHashMap is already an instance of Object: every Java instances inherit from the root class Object.
If you want to transform your HashMap into a specific object such as JSON or a custom object such as com.mycompany.CompData, you have several possibilities depending on your use case:
use DataWeave as mentioned in other answers (require EE)
use a built-in transformer such as Object-to-JSON
implement your own Transformer by extending AbstractTransformer
Se the docs for details: https://docs.mulesoft.com/mule-user-guide/v/3.8/using-transformers
If you may be more specific as to what your use case is I'll gladly refine my answer ;)

You can use either dataweave or json to object transformer.

Related

Generic Json To Object transformer

Is there any way to set the value of "type" attribute of a JsonToObject transformer dynamically ?
For ex, A message header will tell you the target Java Object to which incoming Json payload should be converted to.
Something like,
<int:json-to-object-transformer
input-channel="jsonTransformationChannel" type="headers['targetJavaObject']"
output-channel="payloadTransformationChannel" />
NOTE : "type" attribute does not support SpEL expressions.
Starting with version 3, the JTOT uses similar headers to the Spring AMQP JSON message converters. See JsonHeaders.
For a simple type, set the json__TypeId__ header to the fully qualified class name and do not configure a type.

Spring Integration Object To Map Transformer

I am using SI 4.0 and trying to use object-to-map-transformer as below
<integration:object-to-map-transformer input-channel="inputChannel"
output-channel="outChannel" >
</integration:object-to-map-transformer>
I am sending a object like Person class on the inputChannel. But the moment I fire my test it fails with following error
Caused by: java.lang.IllegalStateException: Neither jackson-databind.jar,
nor jackson-mapper-asl.jar aren't presented in the classpath. at
org.springframework.integration.support.json.JacksonJsonUtils.<clinit>(JacksonJsonUtils.java:41)
I dont understand why it needs jackson. I looked at SI code and can see it needs Jackson class but why is this need - when I simply need to map a simple object to Map?
Thanks
The code to convert object to map looks like:
Map<String,Object> result = this.jsonObjectMapper.fromJson(this.jsonObjectMapper.toJson(payload), Map.class);
Since the out of the box implementation for the JsonObjectMapper is Jackson, it requires that the last one should be presented in the classpath.
We decided to use JSON notation for the Map presentation, since any object in JSON has map-based structure.
If you have another algorithm to do the same, the contribution is welcome!
Or you can simply implement your own Transformer with that logic and use it from generic <transformer>.

ANTLR4. How to detect ParseTree node type?

I was able to get an object of class org.antlr.v4.runtime.tree.ParseTree from my Parser class. Now I'd like to visit every node and generate custom output based on the tree node type. How is it could be done in ANTLR4? There are no methods like getTreeNodeType().
Java provides a getClass() method you can use. Beyond that you'll need to be more specific about what information you need.
Suppose you have Parser.__Context. It has array of child of type ParseTree.
If you have ParseTree as pt:
if(pt instanceof MuaonParser.DefinationContext)
doSomething(pt);

"Generic" #XmlRootElement?

I'm working against a very large API, most of whose return values look like:
<{APIMethodName}Resp>
<ResponseCode></ResponseCode>
<ResponseMessage></ResponseMessage>
</{APIMethodName}Resp>
Is there any sort of JAXB fu that will let me do this with just one class? I'm ok if JAXB ignores the root, just throws it away.
Thanks.
You could use the unmarshal method that take a class parameter. This causes the JAXB implementation to ignore the root element.

How to serialize class that derives from class decorated with DataContract(IsReference=true)?

I have class A that derives from System.Data.Objects.DataClasses.EntityObject.
When I try to serialize using
var a = new A();
DataContractJsonSerializer serializer = new DataContractJsonSerializer(a.GetType());
serializer.WriteObject(Response.OutputStream, a);
I get error
TestController+A._Id' is not marked with OptionalFieldAttribute, thus indicating that it must be serialized. However, 'TestController+A' derives from a class marked with DataContractAttribute and an IsReference setting of 'True'. It is not possible to have required data members on IsReference classes. Either decorate 'TestController+A._Id' with OptionalFieldAttribute, or disable the IsReference setting on the appropriate parent class.
Even if I decorate the field with OptionalFieldAttribute I get
The type 'TestController+A' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.
I cannot modify EntityObject class. I thought to create A_Bag class exactly as A class and fill it and serialize it instead of A, but I think there's more elegant way to do it.
Can you suggest how I can do it?
I think you can use a "data contract surrogate" here (used via the IDataContractSurrogate interface.)
The data contract surrogate is an advanced feature built upon the Data Contract model you're already using. It lets you do type customization and substitution in situations where you want to change how a type is serialized, deserialized, or (if you're dealing with XML) projected into schema.
In your case, the use of IDataContractSurrogate lets you do custom JSON serialization and deserialization on a per-type or per-object basis. An IDataContractSurrogate would provide the methods needed to substitute one type for another by the DataContractSJsonerializer during serialization and deserialization, and you may want to provide a different "special" intermediary type for your scenario.
Hope this helps!
JSON.Net supports serialization of objects marked with IsReference=true.
There is a detailed walkthrough here:
http://dotnet.learningtree.com/2012/04/03/working-with-the-entity-framework-and-the-web-api/

Resources