How to model key:value object in the ProtoBuf - json

I have to compose a protobuf message which should have object with N key:value pairs.
{ "0xc02aaa39b223fe8d4a0e5c4f27ead9083c756cc2": "73185315951404654", "0x111111111117dc0aa78b770fa6a738034120c302": "90483392540197705773", "0xdac17f958d2ee523a2206206944597c13d831ec7": "2599292593", "0x6b175474e89094c44da98b954eedeac495271d0f": "2887251922547121516", "0xa0b86991c6218b36c1d19d224a2e9eb0ce06eb48": "1995436171", "0x2260fac5e5542a773aa44fbcfedf7c193bc2c599": "152669472", "0x0bc529c00c6401aef6d220be8c6ea1667f6ad93e": "78565917807540502", "0x888888888889c00c67689029d7856aac1065ec11": "10015330752170299581" }
Is there a way to compose this JSON?
I tried to use map:
message Reply { map<string, string> balances = 1; }
but ended up with this error message:
Object literal may only specify known properties, and '0xc02aaa39b223fe8d4a0e5c4f27ead9083c756cc2' does not exist in type 'Reply'.

Related

How to get object of object in JSON API response?

I'm using Ionic with Angular. I have a JSON API response and I want to get the items of an object inside an object, in this case, author items.
JSON API response
{
x{
**authors**:{
auth-sara-rts-2022:
Code: "rts"
Contact:{
email:"xx#gmail.com"
phone:"0.."}
[[Prototype]]: Object
auth-sami-sgs-2022:
Code: "sgs"
Contact:{
email:"xx#gmail.com"
phone:"0.."}
[[Prototype]]: Object
[[Prototype]]: Object
}
[[Prototype]]: Object},
y{
yy: "text"
[[Prototype]]: Object}
[[Prototype]]: Object}
}
Here is how to call the API in ts file
callAPI(body: any, header: any): Observable<any> {
return this.http.post(API_URL+API_ENDPOINT,body,header);
}
postAPI(body:any,header) {
this.callAPI(body, header).subscribe(
(data) => { console.log(data.x.authors);
}
);
}
I get a list of authors, and I'd like to access the items in each author's collection (code and contact).
I tried this code, but it returned an undefined value.
console.log(data.x.authors[0]);
The issue you're having is that you are trying to use Array notation (data.x.authors[0]) to access key/value pairs in an Object. In order to transform the authors Object into an Array, there are multiple approaches. I would propose Object.entries, which returns an Array of tuples, each tuple containing the key followed by the value:
const authors = Object.entries(data.x.authors);
console.log(authors[0]); // ['auth-sara-rts-2022', { Code: 'rts', ... }]
Here are the MDN docs for Object.entries():
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

Serialization of scalar JSON in Flutter's Ferry Graphql

I have the following JSON scalar:
"""
The `JSON` scalar type represents JSON values as specified by [ECMA-404](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf).
"""
scalar JSON
which I am trying to convert since my query is accepting input: JSON. When testing using graphql playground, query is JSON object thus the following works:
query {
carts(where: {
owner:{id: "xxx"}
store:{name: "yyy"}
}) {
id
}
}
# query is the starting from the where: {...}
However when using ferry, I have trouble converting the scalar JSON, I've tried to convert it to String and/or Map<String, dynamic> but both are throwing errors. I am trying to write my custom serializer following this link:
https://ferrygraphql.com/docs/custom-scalars/
however it is throwing the same error like when I'm trying converting it to Map<String, dynamic>, I have no idea what to convert to as dart do not have any type of JsonObject and usually Map is suffice for json representation. Any idea guys? I'd appreciate any clues as I've exhausted all of my options and am currently bruteforce trying

How to convert ManagedCursorStreamProvider to JSOn object in mule 4

How to convert ManagedCursorStreamProvider to Json object in mule.
I have written a java method which takes the Json Object as input
Request Payload:
{ a: "one",
b : "two"}
Invoke static
arg0 : payload
Java Function called using invoke static
public static func(JsonObject json){
}
I am getting the following error:
Expected arguments are [com.google.gson.JsonObject jsonObject] and
invocation was attempted with arguments
[org.mule.runtime.core.internal.streaming.bytes.ManagedCursorStreamProvider
arg0].
No suitable transformation was found to match the expected type for
the parameter [jsonObject].
UPDATE:
I have updated my java method to accept String as input.
"Cannot coerce Object { encoding: UTF-8, mediaType: application/json; charset=UTF-8, mimeType: application/json, raw: org.mule.weave.v2.el.SeekableCursorStream#868075a } (org.mule.weave.v2.el.MuleTypedValue#7c0c5e89) to String
1| arg0 : vars.req as String
^^^^^^^^^^^^^^^^^^
Trace:
at main (line: 1, column: 8)" evaluating expression: "arg0 : vars.req as String".
Mule doesn't know how to convert to a GSON JsonObject. You can use DataWeave to transform it into a Java map. Alternatively, you can change the argument of the Java method to String and Mule will transparently convert the stream to a String. Be sure to use the latest version of the Java module.
If you want to convert to a custom type of object you will need to implement it yourself in Java.

Map of Instant to list of objects - jackson serialization/deserialization

I have a spring boot app and the following DTO class:
public class CarDto {
private Map<Instant, List<CarModelDto>> dateToCarModels = new HashMap<>();
// getters/setters omitted for brevity
}
When I make a call from postman it looks like this:
{
"dateToCarModels": {
"1544612555": [{
<obj1>
},
{
<obj2>
}],
"1544785355": [{
<obj1>
}]
}
}
It gives me the following error:
JSON parse error: Cannot deserialize Map key of type
`java.time.Instant` from String "1544612555": Failed to deserialize
java.time.Instant: (java.time.format.DateTimeParseException) Text
'1544612555' could not be parsed at index 0; nested exception is
com.fasterxml.jackson.databind.exc.InvalidFormatException: (...)
That is understandable as I have Instant as a key map (and pass String in json - since JSON keys must be strings).
Is there any better way to fix it than writting own key serializer/deserializer ?

Json response parsing using JavaScript Overlay Types in GWT

I have a query in parsing json response coming from server. We have used gson to serialize a list of objects to a json string in server. In gwt client, we have used JavaScript overlay types to parse it. So my gwt call from client looks like this
//Code snippet where json response is from list of obj from server.
jsonpReqBuiler.requestObject(url, new AsyncCallback<JsArray<MyJsoClass>>() {
onSuccess(final JsArray<MyJsoClass> result) {
////Need a string equivalent of JsArray type to sore???
}
}
We want to store the json response - JsArray into sqllite database as a string. Even though server responds with Json string, it has resulted as JsArray type after internal parsing, now I can store it as a string so that I can read and convert back??
You can use following code:
jsonpReqBuiler.requestObject(url, new AsyncCallback<JsArray<MyJsoClass>>() {
onSuccess(final JsArray<MyJsoClass> result) {
String json = new JSONArray(result).toString();
}
}