You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by Esteban Gínez <eg...@gmail.com> on 2015/08/04 08:21:26 UTC

JsonOutput and pretty print

Hi all.

Lately I have been using JsonOutput to handle my json serialization and I
find that having JsonOutput.toJson overloaded is pretty nifty.
However when trying to pretty print an object the calls look like this
JsonOutput.prettyPrint(JsonOutput.toJson(object))
And that's pretty awkward in my opinion.

I'd like to propose a change the following change
def prettyPrint = true
JsonOutput.toJson(object, prettyPrint)

We would have an overloaded methods of the .toJson call one with a boolean
prettyPrint and the legacy without the boolean parameter(for backwards
compatibility), like so:

 /**
     * @return a JSON object representation for a map
     */
    public static String toJson(Map m) {
        if (m == null) {
            return NULL_VALUE;
        }

        CharBuf buffer = CharBuf.create(255);
        writeMap(m, buffer);

        return buffer.toString();
    }


//New overloaded method(s)
  public static String toJson(Map m, boolean prettyPrint) {
        String buffer = toJson(m);
        if(buffer == null){
            return NULL_VALUE;
        }

        return prettyPrint ? JsonOutput.prettyPrint(buffer.toString()) :
buffer.toString();
    }


Comments? Ideas?

Re: JsonOutput and pretty print

Posted by Keegan Witt <ke...@gmail.com>.
Actually, thinking about this a little more, my suggestion would introduce
an inconsistency for JsonOutput.prettyPrint(String).  All the other
prettyPrints would mean convert to JSON, then pretty print, but
prettyPrint(String) would either assume the String is JSON, or have to
include an extra parameter to communicate whether it is already JSON or
not.  If we were going to do this, your idea is actually better.

-Keegan


On Tue, Aug 4, 2015 at 1:05 PM, Keegan Witt <ke...@gmail.com> wrote:

> If I were to build JsonOutput from scratch today, I'd probably do as you
> propose.  But since we've already gone down the road of a separate method
> for pretty printing, maybe it'd be more consistent to leave
> JsonOutput.toJson() alone and instead add matching JsonOutput.prettyPrint()
> methods.  What's everyone else think?
>
> -Keegan
>
> On Tue, Aug 4, 2015 at 2:21 AM, Esteban Gínez <eg...@gmail.com> wrote:
>
>> JsonOutput
>
>
>

Re: JsonOutput and pretty print

Posted by Keegan Witt <ke...@gmail.com>.
If I were to build JsonOutput from scratch today, I'd probably do as you
propose.  But since we've already gone down the road of a separate method
for pretty printing, maybe it'd be more consistent to leave
JsonOutput.toJson() alone and instead add matching JsonOutput.prettyPrint()
methods.  What's everyone else think?

-Keegan

On Tue, Aug 4, 2015 at 2:21 AM, Esteban Gínez <eg...@gmail.com> wrote:

> JsonOutput

Re: JsonOutput and pretty print

Posted by Jochen Theodorou <bl...@gmx.org>.
Am 04.08.2015 08:21, schrieb Esteban Gínez:
> Hi all.
>
> Lately I have been using JsonOutput to handle my json serialization and
> I find that having JsonOutput.toJson overloaded is pretty nifty.
> However when trying to pretty print an object the calls look like this
> JsonOutput.prettyPrint(JsonOutput.toJson(object))
> And that's pretty awkward in my opinion.
>
> I'd like to propose a change the following change
> def prettyPrint = true
> JsonOutput.toJson(object, prettyPrint)

Used as category your original code reduces to:

object.toJson().prettyPrint()

and I think that is ok.


> We would have an overloaded methods of the .toJson call one with a
> boolean prettyPrint and the legacy without the boolean parameter(for
> backwards compatibility), like so:
>
>   /**
>       * @return a JSON object representation for a map
>       */
>      public static String toJson(Map m) {
>          if (m == null) {
>              return NULL_VALUE;
>          }
>
>          CharBuf buffer = CharBuf.create(255);
>          writeMap(m, buffer);
>
>          return buffer.toString();
>      }
>
>
> //New overloaded method(s)
>    public static String toJson(Map m, boolean prettyPrint) {
>          String buffer = toJson(m);
>          if(buffer == null){
>              return NULL_VALUE;
>          }
>
>          return prettyPrint ? JsonOutput.prettyPrint(buffer.toString())
> : buffer.toString();
>      }
>
>
> Comments? Ideas?

what about the 12+ other variants? You want to add this to each of those?

bye blackdrag

-- 
Jochen "blackdrag" Theodorou
blog: http://blackdragsview.blogspot.com/