Last active
June 20, 2022 08:53
-
-
Save jenslohmann/6a01ef23f80687549d0f to your computer and use it in GitHub Desktop.
Java toString() generator for IntelliJ IDEA that generates JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
$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
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}