Re: [go-nuts] Returning a JSON object saved as a string in a database

2017-08-12 Thread Frits van Bommel
On Friday, August 11, 2017 at 6:43:39 AM UTC+2, Dmitriy Cherchenko wrote:
>
> Just to make sure: When marshalling a struct, will fields of 
> type json.RawMessage be ignored (simply inserted into the JSON object 
> without further processing)?
>

There's some *slight* processing:
- a check that it's valid JSON.
- removing insignificant whitespace.
- optionally: the HTML-escaping described in the documentation of 
json.Marshal  unless you're 
using a json.Encoder with this feature disabled.
but all of that's just a single pass over the string representation while 
copying it to the output 
. There's no 
unmarshal+re-marshal step if that's what you're worried about.

Oh, and if you use json.MarshalIndent() or json.Encoder.SetIndent() there's 
a separate indentation pass, but even that's still just string processing.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Returning a JSON object saved as a string in a database

2017-08-10 Thread Dmitriy Cherchenko
Wow that appears to be exactly the answer I'm looking for! Thank you.

Just to make sure: When marshalling a struct, will fields of 
type json.RawMessage be ignored (simply inserted into the JSON object 
without further processing)?


On Thursday, August 10, 2017 at 7:27:17 PM UTC-7, Jakob Borg wrote:
>
> On 10 Aug 2017, at 22:11, Dmitriy Cherchenko  > wrote:
>
> What is the best way to send the stringified JSON object from the database 
> as JSON and not as a string-type value of a property in the bigger JSON 
> object the server is responding with?
>
>
> Do you mean like this?
>
> https://play.golang.org/p/wUFYoKvDRm
>
> //jb
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Returning a JSON object saved as a string in a database

2017-08-10 Thread Jakob Borg
On 10 Aug 2017, at 22:11, Dmitriy Cherchenko 
mailto:dcherche...@gmail.com>> wrote:

What is the best way to send the stringified JSON object from the database as 
JSON and not as a string-type value of a property in the bigger JSON object the 
server is responding with?

Do you mean like this?

https://play.golang.org/p/wUFYoKvDRm

//jb

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Returning a JSON object saved as a string in a database

2017-08-10 Thread Dmitriy Cherchenko
But then if the JSON is sent as a string property of a bigger object, it'll 
have to be parsed by JavaScript on the browser side. That is undesirable.

On Thursday, August 10, 2017 at 1:21:54 PM UTC-7, Shawn Milochik wrote:
>
> On Thu, Aug 10, 2017 at 4:20 PM, Dmitriy Cherchenko  > wrote:
>
>> Hi Shawn,
>>
>> Thank you for your quick reply!
>>
>> I’m not having any issues retrieving data from the database. I’m only 
>> concerned that the unmarshalling and marshalling processes seem needless 
>> and a little risky especially on big JSON objects.
>>
>
> Sure. If you just want to pull it out of the DB and return it as a string, 
> there's no reason to use the encoding/json package.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Returning a JSON object saved as a string in a database

2017-08-10 Thread Shawn Milochik
On Thu, Aug 10, 2017 at 4:20 PM, Dmitriy Cherchenko 
wrote:

> Hi Shawn,
>
> Thank you for your quick reply!
>
> I’m not having any issues retrieving data from the database. I’m only
> concerned that the unmarshalling and marshalling processes seem needless
> and a little risky especially on big JSON objects.
>

Sure. If you just want to pull it out of the DB and return it as a string,
there's no reason to use the encoding/json package.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Returning a JSON object saved as a string in a database

2017-08-10 Thread Dmitriy Cherchenko
Hi Shawn,

Thank you for your quick reply!

I’m not having any issues retrieving data from the database. I’m only 
concerned that the unmarshalling and marshalling processes seem needless 
and a little risky especially on big JSON objects.


On Thursday, August 10, 2017 at 1:17:02 PM UTC-7, Shawn Milochik wrote:
>
> Seems like something that should "just work." Are you having a problem 
> with this? If so, please show some code.
>
> A string is a string -- if you insert a base64-encoded value (or JSON, or 
> YAML) into a database varchar or text field, you should get the identical 
> string back out, character for character. The fact that it happens to be 
> base64 (or JSON, or YAML), shouldn't matter.
>
> If it comes out as invalid JSON, I'd bet it's being modified by your code 
> before insertion or after retrieval. Either way, show some code and I'll be 
> happy to look at it.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [go-nuts] Returning a JSON object saved as a string in a database

2017-08-10 Thread Shawn Milochik
Seems like something that should "just work." Are you having a problem with
this? If so, please show some code.

A string is a string -- if you insert a base64-encoded value (or JSON, or
YAML) into a database varchar or text field, you should get the identical
string back out, character for character. The fact that it happens to be
base64 (or JSON, or YAML), shouldn't matter.

If it comes out as invalid JSON, I'd bet it's being modified by your code
before insertion or after retrieval. Either way, show some code and I'll be
happy to look at it.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[go-nuts] Returning a JSON object saved as a string in a database

2017-08-10 Thread Dmitriy Cherchenko
>From my server I'd like to respond with JSON content—a JSON object that 
will contain as one of its properties a stringified JSON object that will 
be retrieved from the database. What is the best way to send the 
stringified JSON object from the database as JSON and not as a string-type 
value of a property in the bigger JSON object the server is responding 
with? I know I can unmarshal the string into a Go struct and then add it to 
the bigger struct that will be marshalled into JSON and sent off as the 
response. But that's a lot of extra work for the server (and could 
potentially introduce a small error along the way).

Really all that needs to be done to the stringified JSON object retrieved 
from the database is that it shouldn't have quotes around it. What's the 
best way to avoid the unmarshal-and-then-marshal technique?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.