Skip to content

Instantly share code, notes, and snippets.

@IAFahim
Forked from jenslohmann/toString Generator
Last active September 12, 2021 07:24
Show Gist options
  • Save IAFahim/7ed69c2de6af0a8cddabf15c69d7860d to your computer and use it in GitHub Desktop.
Save IAFahim/7ed69c2de6af0a8cddabf15c69d7860d to your computer and use it in GitHub Desktop.
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
}
@IAFahim
Copy link
Author

IAFahim commented Sep 12, 2021

Pros:

no space

Cons:

double quote in String and dead json

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