Modifying ServiceStack's JSON output - serialization

I have to build a REST service with ServiceStack; the responses must have a certain format. Both JSON and XML are to be supported. The standard serializers do not return the response in the format I need.
For JSON, it would be enough to wrap the result, e.g. if a function returns a list of Site objects, the JSON serializer gives me [{...}, ...], but I need {"Sites": [{...}, ...]}. The requested content-type would be "Sites+json" in this case. For other functions, "Sites" would be replaced by something else.
How can I achieve this?
Edit:
The XML has to be the direct "translation" of the JSON, like
<Sites>...</Sites> instead of {"Sites":...}.
The standard XML serialization works differently, it always puts in the data type as well.
Has anyone an idea how to do this? I guess I have to write my own XML serializer and map all my XML types (like Sites+xml,...) to it?

Related

what are the various ways to fetch the properties from payload?

What is the difference between reading the properties from payload. for example there is a property in the payload which is named as con_id. when i read this property like this #[payload.con_id] then it is coming as null. where as #[payload.'con_id'] is returning the value.
few other notations which i know of is #[payload['con_id']] or #[json:con_id]
which one should be used at which scenario? if there are any special cases to use any specific notation then please let me know the scenario also.
Also, what is the common notation that has to be used from a mule soft platform supported point of view.
In Mule 3 any of those syntax are valid. Except the json: evaluator is for querying json documents where as the others are for querying maps/objects. Also the json: evaluator is deprecated in Mule 3 in favor of transforming to a map and using the MEL expressions below.
payload.property
payload.'property'
payload['property']
The reason the first fails in your case, is beacaue of the special character '_'. The underscore forces the field name to be wrapped in quotes.
Typically the . notation is preferred over the [''] as its shorter for accessing map fields. And then simply wrap property names in '' for any fields with special chars.
Note in Mule 4, you don't need to transform to a map/object first. Dataweave expression replace MEL as the expression language and allow you to directly query json or any type of payload without transforming to a map first.

Use encoded object query param for GET request

While designing the API with with the team a suggestion was forwarded in regards to some complex query parameters that we sent which need to be encoded as objects, arrays of objects, etc. Suppose I have a route GET /resource/ and I want to apply a set of filters directly in the query params. The object literal structure of this filter would be something like
filter: {
field1: {
contains: 'value',
notin: ['value2', 'value3']
},
field2: {
greaterThan: 10
}
}
Encoding this in the url, via a query string parser such as the qs node module that express.js uses internally, would be cheap on the backend. However 1) The generated url is very hard to read, if a client wants to connect with the API he would need to use an encoding library and 2) I don't think I ever encountered the use of query params like this, it looks a little bit of overengineered and I'm not sure how used it is and if it is really safe.
The example above would yield query params such as:
GET /resource/?field1%5Bcontains%5D=value&field1%5Bnotin%5D%5B0%5D=value2&field1%5Bnotin%5D%5B1%5D=value3&field2%5BgreaterThan%5D=10
Does this practice of sending url query parameters that happen to be complex objects have some standards or best practices?
We implemented a different solution for filtering, when the list of possible parameters was very long. We ended up doing it in two steps, posting the filter and returning a filter ID. The filter ID could then be used in the GET query.
We had trouble finding any best practices for this.

Woodstox default entities replaced

I have the following tag in a webservice xml response:
<text>"><&</text>
And this is reported in my characters method as "><&, but I need to be reported as it is ("><&).
I've set XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES to Boolean.FALSE, but it doesn't work.
Can anyone help me?
Thanks in advance.
Joan.
No, this is not possible with Woodstox. Handling of character entities, and pre-defined entities (lt, gt, amp, apos, quot) is automatic and is required of XML parsers.
There are some XML parsers that expose underlying raw buffer contents; I think xpp3 does that. You could try it instead. But none of Stax implementations that I know of supports such access, nor SAX parsers.

Noflo .fbp array initiallizer

I'm using noflo and am trying to send an array as an initiallizer. There doesn't seem to be a supported (or at least documented) way to do this.
I'm currently using:
'["Kicker"]' -> IN Nodes(strings/ParseJson)
'{"in":"go!"}' -> IN Config(strings/ParseJson)
Nodes() OUT -> NODES MyComponent(noflotest/Universe)
Config OUT -> CONFIG MyComponent()
Is there a better way to do this?
Currently arrays and other complicated data structures are not supported in the .fbp syntax. There is a feature request about this.
Right now you have three options:
If FBP parser accepts your string (see the matching rules), you can first send it to the strings/ParseJson component to turn it to the appropriate data structure
Reading the value from a JSON or YAML file and passing it through the appropriate parser component
Converting your graph to the JSON graph format

oData operation consumption with objective-C

I have a really simple WCF service operation GetCurrentBalance. It returns a decimal.
I also have the odatagen generated entity files included in the project, which contains an implementation of the GetCurrentBalance operation returning a string. Calling this method returns me an XML string with the desired value in it.
I also tried using executeServiceOperation method in the generated class and pass in the operation name as a parameter, the returned value again is the same XML string.
Is there a way to extract this value? Or do I have to write a custom parser for it?
Thanks in advance.
Without further informations, if the returned value is a formatted XML string you may try extracting the value using XPath queries, have a look at this to get you started