Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Java toString() generator for IntelliJ IDEA that generates JSON
public java.lang.String toString() {
#if ( $members.size() > 0 )
#set ( $i = 0 )
return "{\"_class\":\"$classname\", " +
#foreach( $member in $members )
#set ( $i = $i + 1 )
#if ( $i == $members.size() )
#set ( $postfix = "+" )
#else
#set ( $postfix = "+ "", "" + " )
#end
#if ( $member.collection )
"\"$member.name\":" + ($member.accessor == null ? "null" : java.util.Arrays.toString($member.accessor.toArray())) $postfix
#elseif ( $member.objectArray )
"\"$member.name\":" + java.util.Arrays.toString($member.accessor) $postfix
#elseif ( $member.primitiveArray )
"\"$member.name\":" + java.util.Arrays.toString($member.accessor) $postfix
#elseif ( $member.object )
#if ($member.typeQualifiedName.contains("java.lang"))
"\"$member.name\":" + ($member.accessor == null ? "null" : "\"" + $member.accessor + "\"" ) $postfix
#else
"\"$member.name\":" + ($member.accessor == null ? "null" : $member.accessor ) $postfix
#end
#else
"\"$member.name\":\"" + $member.accessor + "\"" $postfix
#end
#end
"}";
#else
return "{\"_class\":\"$classname\"}";
#end
}
@scottpineapple
Copy link

scottpineapple commented May 6, 2015

I believe line 24 should be
"}";
not
""}"";

@viktortaranenko
Copy link

viktortaranenko commented May 12, 2015

scottyknows knows

@jenslohmann
Copy link
Author

jenslohmann commented May 22, 2015

Fixed :-)

@hhimanshu
Copy link

hhimanshu commented Aug 5, 2016

how to use it?

@jenslohmann
Copy link
Author

jenslohmann commented Dec 16, 2016

Ctrl-N (or Code -> Generate...), select "toString()". Here you can add the macro using button Settings, pane Templates.
(One way) to use the macro hit Ctrl-N, select "toString()", select the macro, click OK.

@cheny1ran
Copy link

cheny1ran commented Mar 15, 2017

I think Map type Object is not supposed to be surrounded with ""

@msaiducar
Copy link

msaiducar commented Jan 17, 2018

Shouldn't treat Date objects as string? I add " || $member.date" to if check in line#19

@njchandu
Copy link

njchandu commented Jun 26, 2018

Is there a way to pretty print this?

@xuhuanfeng
Copy link

xuhuanfeng commented Oct 22, 2018

Hi, There may be a bug for list member , for example, said I have an obj contain a List<String> list, above code will generate "name": [mem1, ,mem2, ...] which actually no a json string.

@lourencomcviana
Copy link

lourencomcviana commented Jun 19, 2020

One little detail, if the string has double quotes inside this generator will give an error.
Example: my chocolate was "delicious" but it was not
this will be cast to "my chocolate was "delicious" but it was not" and is not a valid string

i solved this using a very lazy method

.....
       #elseif ( $member.object )
            #if ($member.typeQualifiedName.contains("java.lang"))
            "\"$member.name\":" + ($member.accessor == null ? "null" : "\"" + $member.accessor + "\"" ) $postfix
            #else
            "\"$member.name\":" + ($member.accessor == null ? "null" : $member.accessor .toString().replaceAll("\"","'") ) $postfix
            #end
        #else
....

@fabiojb
Copy link

fabiojb commented Jul 24, 2020

Line 13 has an invalid space that breaks the script:

... java.util.Arrays.toString($member.accessor .toArray())) ....

should be changed to

... java.util.Arrays.toString($member.accessor.toArray())) ...

@IAFahim
Copy link

IAFahim commented Sep 12, 2021

can't we fix for number
{"_class":"Residents", "name":"John Smith", "nid":"19752533333333333", "salary":"30000.0"}
to
{"_class":"Residents", "name":"John Smith", "nid":19752533333333333, "salary":30000.0}

@Attacktive
Copy link

Attacktive commented Mar 10, 2022

$member.accessor.toArray() needs to be something like $member.accessor .toArray() at line 13.
$member.accessor.toArray() gets printed literally otherwise.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment