I've been trying for a while to get a byte[] from my database blob and then parse it to json and send it to my clients. I first get the blob from the database and then I get the byte array by doing:
MyObject temp = new MyObject()
Blob icon = dbResult.getBlob(1);
temp.setIcon(icon.getBytes(1, (int)icon.length()));
Later on I parse MyObject to a json string which I send back to clients. But it seems that the json string becomes pretty much corrupted when parsing the byte[].
An example of how it might look:
[{"icon":"/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAFQAPwDASIAAhEBAxEB/8QAHAAAAQUBAQEAAAAAAAAAAAAAAgEDBAUGAAcI
I parsed this to json in my webservice which is a jersery webservice. When i try to parse it from json into a class object at client side, it throws IllegalStateException(Gson).
Anyone knows what this is about and what i'm doing wrong?
If you try to transfer binary data using json, you better explicitly encode it with base64 and then decode it on receiver's side. JSON isn't designed to wrap binaries, so the problems when trying to make this were expectable.
Related
New to Dart. It seems that .transform(JsonDecoder()) will hang until the stream is closed or throw an error if it starts to see a new Json object. I could cache the entire strings and parse them that way, but I would like to take advantage of the stream an not store more than is needed in memory.
Is there a way to get the JsonDecoder to push an object to the sink as soon as it gets a complete valid Json Object? I've tried extending some of the internal classes, but only got a private library error.
https://github.com/dart-lang/sdk/blob/1278bd5adb6a857580f137e47bc521976222f7b9/sdk/lib/_internal/vm/lib/convert_patch.dart#L1500 . This seems to be the relevant code and it's really a pain in my butt. Would I need to create a dummy stream or something?
If the input is newline separated, you can do:
Stream jsonObjects = inputStream
.transform(utf8.decoder) // if incoming is bytes.
.transform(const LineSplitter())
.map(jsonDecode);
The JsonDecoder converter only works on a single JSON value, because the JSON grammar doesn't allow more than one value in a JSON source text.
The LineSplitter will buffer until it has an entire line, then emit one line at a time, so if each JSON message is on a line by itself, that makes each event from the line-splitted stream a complete JSON value.
I'm using the-sett/elm-aws-core to get information from the AWS API, which unfortunately is very very inconsistent. Most of the endpoints return JSON and that works fine with that lib, which takes a JSON Decoder to make a request, but the EC2 endpoint returns XML (because why not).
The lib doesn't have any options not to decode JSON as far as I can tell, which does not work at all :
let ec2 region = Service.defineRegional "ec2" "2016-11-15" Service.QUERY Service.SignV4 (Service.setXmlNamespace "https://ec2.amazonaws.com/doc/2016-11-15/") region in
let params = [("Action", "DescribeImages"), ("Version", "2016-11-15"), ("Owner.1", "self")] in
Http.request "DescribeImages" GET "/" Http.emptyBody JSONDECODERHERE |> Http.addQuery params |> Http.send (ec2 region) creds |> Task.attempt msg
Failed : Problem with the given value:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<DescribeImagesResponse .......
As you can see in there you need to pass a JSON Decoder to Http.request, but that, of course, fails when receiving XML. Is there a way to build a "fake" JSON decoder that would actually do nothing and just pass on the raw string? I tried using Decode. string but that's still actually decoding it, which fails. If there is a way I could then run an XML decoder manually on it in my update function, which would be fine.
Thank you
It's not possible to make a "fake" decoder that does what you want, because the problem isn't with the decoding. The problem is with the parsing, which is done before decoding. Parsing is the process of converting the string into a data structure typically called an Abstract Syntax Tree (AST), but since Elm compiles to JavaScript and JSON is also a subset of JavaScript the parse result of is really just a JavaScript object. Decoding is the process of turning that untyped data structure into a properly typed data structure.
It is therefore not possible to accomplish what you want with this API. Most likely you'll need to build the http request yourself and use elm/http directly.
I have never encountered this sort of collection or object before until now (its the response from a request to Google-Cloud-Vision API).
I wrote a class that uses the API and does what I want correctly. However the only way that I can extract/manipulate data in the response is by using this module:
from google.protobuf.json_format import MessageToJson
I basically serialized the protobuff into a string and then used regex to get the data that I want.
There MUST be a better way than this. Any suggestions? I was hoping to have the API response give me a json dict or json dict of dicts etc... All I could come up with was turning the response into a string though.
Here is the file from the github repository:
image_analyzer.py
Thank you all in advance.
The built in json module will parse the string into a dictionary, like json.loads(MessageToJson(response1)).
You can just access the fields in the message object directly, e.g.:
response1 = vision_client.face_detection(image=image)
print(response1)
print(response1.face_annotations[0].detection_confidence)
I was expecting a JSON string while testing an API using Postman, but instead got this:
{city=Shanghai, work=112-454-7895, fax=788-899-7899}
Obviously I cannot put that into Google and ask what format is it, hence I am asking it here. Postman also says it is a 'bad string'.
I have never seen the above data serialization format. If someone can point the format out to me I would be able to find and use a converter. Additional suggestion with converting it to POJO or JSON are welcome as well.
I figured out that the above format is a stringified version of a HashMap<String, String>, and that there is no direct way to convert it into JSON/POJO. Converting it into json string requires additional work but is fairly straightforward.
what would be the best way to send string (unicode text) & array-of-string data (several hundreds characters, for now formatted as a single Json object) from an HTML page to a PHP server document (to be then recorded in a db)? I'm pretty much new at Json and I'm not sure how to send a Json object server side with an Ajax call.
Right now, my idea would be to somehow serialize my Json object into a string, then send that string as a parameter to a POST Ajax call, but again, I'm not sure if this is the right way or how to serialize Json.
I don't use jquery at this time.
You can use JSON.stringify as you suggested.
JSON.stringify
But why do you want to serialize as JSON on the client side as opposed to sending as is to the server and converting to JSON on the server side? Then you could use a JSON library in whatever language your server supports.