Sending byte array in a JSON format

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Sending byte array in a JSON format

Juraj Kubelka
Hi,

I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 

I can obtain the whole Gist executing:

STONJSON fromString: 
(ZnClient new
accept: 'application/vnd.github.v3+json';
get).

I can get PNG contents executing:

pngData := (ZnEasy get:
((((STONJSON fromString: 
(ZnClient new
accept: 'application/vnd.github.v3+json';
get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
PNGReadWriter formFromStream: rawPng readStream.

But the PNG image is part of the Gist query and can be retrieved by:

pngContent := ((((STONJSON fromString: 
(ZnClient new
accept: 'application/vnd.github.v3+json';
get)) at: 'files') at: 'thumbnail.png') at: 'content').

"As pngContent is a WideString, I cannot use:"
PNGReadWriter formFromStream: pngContent readStream.

How can I read the PNG image from the pngContent? Any idea? 
And the reverse question: How can I send the PNG bytes using JSON format? 

Thanks!
Juraj

Reply | Threaded
Open this post in threaded view
|

Re: Sending byte array in a JSON format

Sven Van Caekenberghe-2
I am puzzled by how they actually encoded the PNG as a String, I tried a couple of alternatives but I could not get binary data out of it so that it parsed successfully as PNG.

If I would have to encode binary data in JSON I would use Base64 encoding (but alternatives exist).

> On 24 Apr 2017, at 20:36, Juraj Kubelka <[hidden email]> wrote:
>
> Hi,
>
> I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
> I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 
>
> I can obtain the whole Gist executing:
>
> STONJSON fromString:
> (ZnClient new
> url: 'https://api.github.com/gists/5503544';
> accept: 'application/vnd.github.v3+json';
> get).
>
> I can get PNG contents executing:
>
> pngData := (ZnEasy get:
> ((((STONJSON fromString:
> (ZnClient new
> url: 'https://api.github.com/gists/5503544';
> accept: 'application/vnd.github.v3+json';
> get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
> PNGReadWriter formFromStream: rawPng readStream.
>
> But the PNG image is part of the Gist query and can be retrieved by:
>
> pngContent := ((((STONJSON fromString:
> (ZnClient new
> url: 'https://api.github.com/gists/5503544';
> accept: 'application/vnd.github.v3+json';
> get)) at: 'files') at: 'thumbnail.png') at: 'content').
>
> "As pngContent is a WideString, I cannot use:"
> PNGReadWriter formFromStream: pngContent readStream.
>
> How can I read the PNG image from the pngContent? Any idea?
> And the reverse question: How can I send the PNG bytes using JSON format?
>
> Thanks!
> Juraj
>


Reply | Threaded
Open this post in threaded view
|

Re: Sending byte array in a JSON format

Peter Uhnak
Maybe the content is not properly stored in the JSON on github' side? But you can use base64 in `accept:` to make it work.

json := STONJSON fromString: (ZnClient new
                      url: 'https://api.github.com/gists/5503544';
                      accept: 'application/vnd.github.v3.base64+json';
                      get).
b64 := ((json at: 'files') at: 'thumbnail.png') at: 'content'.
PNGReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: content readStream).

Peter


On Wed, Apr 26, 2017 at 04:50:04PM +0200, Sven Van Caekenberghe wrote:

> I am puzzled by how they actually encoded the PNG as a String, I tried a couple of alternatives but I could not get binary data out of it so that it parsed successfully as PNG.
>
> If I would have to encode binary data in JSON I would use Base64 encoding (but alternatives exist).
>
> > On 24 Apr 2017, at 20:36, Juraj Kubelka <[hidden email]> wrote:
> >
> > Hi,
> >
> > I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
> > I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 
> >
> > I can obtain the whole Gist executing:
> >
> > STONJSON fromString:
> > (ZnClient new
> > url: 'https://api.github.com/gists/5503544';
> > accept: 'application/vnd.github.v3+json';
> > get).
> >
> > I can get PNG contents executing:
> >
> > pngData := (ZnEasy get:
> > ((((STONJSON fromString:
> > (ZnClient new
> > url: 'https://api.github.com/gists/5503544';
> > accept: 'application/vnd.github.v3+json';
> > get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
> > PNGReadWriter formFromStream: rawPng readStream.
> >
> > But the PNG image is part of the Gist query and can be retrieved by:
> >
> > pngContent := ((((STONJSON fromString:
> > (ZnClient new
> > url: 'https://api.github.com/gists/5503544';
> > accept: 'application/vnd.github.v3+json';
> > get)) at: 'files') at: 'thumbnail.png') at: 'content').
> >
> > "As pngContent is a WideString, I cannot use:"
> > PNGReadWriter formFromStream: pngContent readStream.
> >
> > How can I read the PNG image from the pngContent? Any idea?
> > And the reverse question: How can I send the PNG bytes using JSON format?
> >
> > Thanks!
> > Juraj
> >
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Sending byte array in a JSON format

Peter Uhnak
In reply to this post by Sven Van Caekenberghe-2
It is also possible that ZnEasy is doing some encoding transformations that breaks it; after all, you are retrieving binary data as text data, so encoding should be applied on it.

Peter

On Wed, Apr 26, 2017 at 04:50:04PM +0200, Sven Van Caekenberghe wrote:

> I am puzzled by how they actually encoded the PNG as a String, I tried a couple of alternatives but I could not get binary data out of it so that it parsed successfully as PNG.
>
> If I would have to encode binary data in JSON I would use Base64 encoding (but alternatives exist).
>
> > On 24 Apr 2017, at 20:36, Juraj Kubelka <[hidden email]> wrote:
> >
> > Hi,
> >
> > I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
> > I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 
> >
> > I can obtain the whole Gist executing:
> >
> > STONJSON fromString:
> > (ZnClient new
> > url: 'https://api.github.com/gists/5503544';
> > accept: 'application/vnd.github.v3+json';
> > get).
> >
> > I can get PNG contents executing:
> >
> > pngData := (ZnEasy get:
> > ((((STONJSON fromString:
> > (ZnClient new
> > url: 'https://api.github.com/gists/5503544';
> > accept: 'application/vnd.github.v3+json';
> > get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
> > PNGReadWriter formFromStream: rawPng readStream.
> >
> > But the PNG image is part of the Gist query and can be retrieved by:
> >
> > pngContent := ((((STONJSON fromString:
> > (ZnClient new
> > url: 'https://api.github.com/gists/5503544';
> > accept: 'application/vnd.github.v3+json';
> > get)) at: 'files') at: 'thumbnail.png') at: 'content').
> >
> > "As pngContent is a WideString, I cannot use:"
> > PNGReadWriter formFromStream: pngContent readStream.
> >
> > How can I read the PNG image from the pngContent? Any idea?
> > And the reverse question: How can I send the PNG bytes using JSON format?
> >
> > Thanks!
> > Juraj
> >
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Sending byte array in a JSON format

Sven Van Caekenberghe-2
In reply to this post by Peter Uhnak

> On 26 Apr 2017, at 17:21, Peter Uhnak <[hidden email]> wrote:
>
> Maybe the content is not properly stored in the JSON on github' side? But you can use base64 in `accept:` to make it work.
>
> json := STONJSON fromString: (ZnClient new
>                      url: 'https://api.github.com/gists/5503544';
>                      accept: 'application/vnd.github.v3.base64+json';
>                      get).
> b64 := ((json at: 'files') at: 'thumbnail.png') at: 'content'.
> PNGReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: content readStream).

Ah, great that you found a way to force the server to send the data in a more sensible way !

Using the latest code from Zn & NeoJSON, combining with another recent thread (accessing nested dictionaries), this could be written as:

json := NeoJSONObject fromString: (ZnClient new
                url: 'https://api.github.com/gists/5503544';
                accept: 'application/vnd.github.v3.base64+json';
                get).
b64 := json atPath: #('files' 'thumbnail.png' 'content').
PNGReadWriter formFromStream: (ZnBase64Encoder new decode: b64) readStream.

Sven

> Peter
>
>
> On Wed, Apr 26, 2017 at 04:50:04PM +0200, Sven Van Caekenberghe wrote:
>> I am puzzled by how they actually encoded the PNG as a String, I tried a couple of alternatives but I could not get binary data out of it so that it parsed successfully as PNG.
>>
>> If I would have to encode binary data in JSON I would use Base64 encoding (but alternatives exist).
>>
>>> On 24 Apr 2017, at 20:36, Juraj Kubelka <[hidden email]> wrote:
>>>
>>> Hi,
>>>
>>> I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
>>> I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 
>>>
>>> I can obtain the whole Gist executing:
>>>
>>> STONJSON fromString:
>>> (ZnClient new
>>> url: 'https://api.github.com/gists/5503544';
>>> accept: 'application/vnd.github.v3+json';
>>> get).
>>>
>>> I can get PNG contents executing:
>>>
>>> pngData := (ZnEasy get:
>>> ((((STONJSON fromString:
>>> (ZnClient new
>>> url: 'https://api.github.com/gists/5503544';
>>> accept: 'application/vnd.github.v3+json';
>>> get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
>>> PNGReadWriter formFromStream: rawPng readStream.
>>>
>>> But the PNG image is part of the Gist query and can be retrieved by:
>>>
>>> pngContent := ((((STONJSON fromString:
>>> (ZnClient new
>>> url: 'https://api.github.com/gists/5503544';
>>> accept: 'application/vnd.github.v3+json';
>>> get)) at: 'files') at: 'thumbnail.png') at: 'content').
>>>
>>> "As pngContent is a WideString, I cannot use:"
>>> PNGReadWriter formFromStream: pngContent readStream.
>>>
>>> How can I read the PNG image from the pngContent? Any idea?
>>> And the reverse question: How can I send the PNG bytes using JSON format?
>>>
>>> Thanks!
>>> Juraj
>>>
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: Sending byte array in a JSON format

Sven Van Caekenberghe-2
In reply to this post by Peter Uhnak

> On 26 Apr 2017, at 17:25, Peter Uhnak <[hidden email]> wrote:
>
> It is also possible that ZnEasy is doing some encoding transformations that breaks it; after all, you are retrieving binary data as text data, so encoding should be applied on it.

JSON is text, using a specific encoding indeed. Since the JSON comes in and is parsed OK, it is safe to assume the textual decoding went fine.

The problem is that the server sends binary data in a string without us knowing how it encoded them. That is why the base64 approach is better.

> Peter
>
> On Wed, Apr 26, 2017 at 04:50:04PM +0200, Sven Van Caekenberghe wrote:
>> I am puzzled by how they actually encoded the PNG as a String, I tried a couple of alternatives but I could not get binary data out of it so that it parsed successfully as PNG.
>>
>> If I would have to encode binary data in JSON I would use Base64 encoding (but alternatives exist).
>>
>>> On 24 Apr 2017, at 20:36, Juraj Kubelka <[hidden email]> wrote:
>>>
>>> Hi,
>>>
>>> I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
>>> I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 
>>>
>>> I can obtain the whole Gist executing:
>>>
>>> STONJSON fromString:
>>> (ZnClient new
>>> url: 'https://api.github.com/gists/5503544';
>>> accept: 'application/vnd.github.v3+json';
>>> get).
>>>
>>> I can get PNG contents executing:
>>>
>>> pngData := (ZnEasy get:
>>> ((((STONJSON fromString:
>>> (ZnClient new
>>> url: 'https://api.github.com/gists/5503544';
>>> accept: 'application/vnd.github.v3+json';
>>> get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
>>> PNGReadWriter formFromStream: rawPng readStream.
>>>
>>> But the PNG image is part of the Gist query and can be retrieved by:
>>>
>>> pngContent := ((((STONJSON fromString:
>>> (ZnClient new
>>> url: 'https://api.github.com/gists/5503544';
>>> accept: 'application/vnd.github.v3+json';
>>> get)) at: 'files') at: 'thumbnail.png') at: 'content').
>>>
>>> "As pngContent is a WideString, I cannot use:"
>>> PNGReadWriter formFromStream: pngContent readStream.
>>>
>>> How can I read the PNG image from the pngContent? Any idea?
>>> And the reverse question: How can I send the PNG bytes using JSON format?
>>>
>>> Thanks!
>>> Juraj
>>>
>>
>>
>


Reply | Threaded
Open this post in threaded view
|

Re: Sending byte array in a JSON format

Juraj Kubelka
In reply to this post by Sven Van Caekenberghe-2
Hi,

That’s great! Thank you a lot for the information :-)

Juraj

> On Apr 27, 2017, at 09:52, Sven Van Caekenberghe <[hidden email]> wrote:
>
>
>> On 26 Apr 2017, at 17:21, Peter Uhnak <[hidden email]> wrote:
>>
>> Maybe the content is not properly stored in the JSON on github' side? But you can use base64 in `accept:` to make it work.
>>
>> json := STONJSON fromString: (ZnClient new
>>                     url: 'https://api.github.com/gists/5503544';
>>                     accept: 'application/vnd.github.v3.base64+json';
>>                     get).
>> b64 := ((json at: 'files') at: 'thumbnail.png') at: 'content'.
>> PNGReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: content readStream).
>
> Ah, great that you found a way to force the server to send the data in a more sensible way !
>
> Using the latest code from Zn & NeoJSON, combining with another recent thread (accessing nested dictionaries), this could be written as:
>
> json := NeoJSONObject fromString: (ZnClient new
> url: 'https://api.github.com/gists/5503544';
> accept: 'application/vnd.github.v3.base64+json';
> get).
> b64 := json atPath: #('files' 'thumbnail.png' 'content').
> PNGReadWriter formFromStream: (ZnBase64Encoder new decode: b64) readStream.
>
> Sven
>
>> Peter
>>
>>
>> On Wed, Apr 26, 2017 at 04:50:04PM +0200, Sven Van Caekenberghe wrote:
>>> I am puzzled by how they actually encoded the PNG as a String, I tried a couple of alternatives but I could not get binary data out of it so that it parsed successfully as PNG.
>>>
>>> If I would have to encode binary data in JSON I would use Base64 encoding (but alternatives exist).
>>>
>>>> On 24 Apr 2017, at 20:36, Juraj Kubelka <[hidden email]> wrote:
>>>>
>>>> Hi,
>>>>
>>>> I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
>>>> I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 
>>>>
>>>> I can obtain the whole Gist executing:
>>>>
>>>> STONJSON fromString:
>>>> (ZnClient new
>>>> url: 'https://api.github.com/gists/5503544';
>>>> accept: 'application/vnd.github.v3+json';
>>>> get).
>>>>
>>>> I can get PNG contents executing:
>>>>
>>>> pngData := (ZnEasy get:
>>>> ((((STONJSON fromString:
>>>> (ZnClient new
>>>> url: 'https://api.github.com/gists/5503544';
>>>> accept: 'application/vnd.github.v3+json';
>>>> get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
>>>> PNGReadWriter formFromStream: rawPng readStream.
>>>>
>>>> But the PNG image is part of the Gist query and can be retrieved by:
>>>>
>>>> pngContent := ((((STONJSON fromString:
>>>> (ZnClient new
>>>> url: 'https://api.github.com/gists/5503544';
>>>> accept: 'application/vnd.github.v3+json';
>>>> get)) at: 'files') at: 'thumbnail.png') at: 'content').
>>>>
>>>> "As pngContent is a WideString, I cannot use:"
>>>> PNGReadWriter formFromStream: pngContent readStream.
>>>>
>>>> How can I read the PNG image from the pngContent? Any idea?
>>>> And the reverse question: How can I send the PNG bytes using JSON format?
>>>>
>>>> Thanks!
>>>> Juraj
>>>>
>>>
>>>
>>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Sending byte array in a JSON format

Sven Van Caekenberghe-2

> On 27 Apr 2017, at 14:57, Juraj Kubelka <[hidden email]> wrote:
>
> Hi,
>
> That’s great! Thank you a lot for the information :-)

Well, it is Peter who came up with the alternative request. All credit to him.

> Juraj
>
>> On Apr 27, 2017, at 09:52, Sven Van Caekenberghe <[hidden email]> wrote:
>>
>>
>>> On 26 Apr 2017, at 17:21, Peter Uhnak <[hidden email]> wrote:
>>>
>>> Maybe the content is not properly stored in the JSON on github' side? But you can use base64 in `accept:` to make it work.
>>>
>>> json := STONJSON fromString: (ZnClient new
>>>                    url: 'https://api.github.com/gists/5503544';
>>>                    accept: 'application/vnd.github.v3.base64+json';
>>>                    get).
>>> b64 := ((json at: 'files') at: 'thumbnail.png') at: 'content'.
>>> PNGReadWriter formFromStream: (Base64MimeConverter mimeDecodeToBytes: content readStream).
>>
>> Ah, great that you found a way to force the server to send the data in a more sensible way !
>>
>> Using the latest code from Zn & NeoJSON, combining with another recent thread (accessing nested dictionaries), this could be written as:
>>
>> json := NeoJSONObject fromString: (ZnClient new
>> url: 'https://api.github.com/gists/5503544';
>> accept: 'application/vnd.github.v3.base64+json';
>> get).
>> b64 := json atPath: #('files' 'thumbnail.png' 'content').
>> PNGReadWriter formFromStream: (ZnBase64Encoder new decode: b64) readStream.
>>
>> Sven
>>
>>> Peter
>>>
>>>
>>> On Wed, Apr 26, 2017 at 04:50:04PM +0200, Sven Van Caekenberghe wrote:
>>>> I am puzzled by how they actually encoded the PNG as a String, I tried a couple of alternatives but I could not get binary data out of it so that it parsed successfully as PNG.
>>>>
>>>> If I would have to encode binary data in JSON I would use Base64 encoding (but alternatives exist).
>>>>
>>>>> On 24 Apr 2017, at 20:36, Juraj Kubelka <[hidden email]> wrote:
>>>>>
>>>>> Hi,
>>>>>
>>>>> I was playing with GitHub Gist API and I have queried the following Gist: https://gist.github.com/mbostock/5503544
>>>>> I was interested how the PNG image is returned: https://gist.github.com/mbostock/5503544#file-thumbnail-png 
>>>>>
>>>>> I can obtain the whole Gist executing:
>>>>>
>>>>> STONJSON fromString:
>>>>> (ZnClient new
>>>>> url: 'https://api.github.com/gists/5503544';
>>>>> accept: 'application/vnd.github.v3+json';
>>>>> get).
>>>>>
>>>>> I can get PNG contents executing:
>>>>>
>>>>> pngData := (ZnEasy get:
>>>>> ((((STONJSON fromString:
>>>>> (ZnClient new
>>>>> url: 'https://api.github.com/gists/5503544';
>>>>> accept: 'application/vnd.github.v3+json';
>>>>> get)) at: 'files') at: 'thumbnail.png') at: 'raw_url')) contents.
>>>>> PNGReadWriter formFromStream: rawPng readStream.
>>>>>
>>>>> But the PNG image is part of the Gist query and can be retrieved by:
>>>>>
>>>>> pngContent := ((((STONJSON fromString:
>>>>> (ZnClient new
>>>>> url: 'https://api.github.com/gists/5503544';
>>>>> accept: 'application/vnd.github.v3+json';
>>>>> get)) at: 'files') at: 'thumbnail.png') at: 'content').
>>>>>
>>>>> "As pngContent is a WideString, I cannot use:"
>>>>> PNGReadWriter formFromStream: pngContent readStream.
>>>>>
>>>>> How can I read the PNG image from the pngContent? Any idea?
>>>>> And the reverse question: How can I send the PNG bytes using JSON format?
>>>>>
>>>>> Thanks!
>>>>> Juraj
>>>>>
>>>>
>>>>
>>>
>>
>>
>
>