[ 
https://issues.apache.org/jira/browse/GROOVY-8849?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16676503#comment-16676503
 ] 

Paul King edited comment on GROOVY-8849 at 11/6/18 10:20 AM:
-------------------------------------------------------------

I'd recommend using the 
[JsonGenerator.Options.addConverter|http://docs.groovy-lang.org/latest/html/api/groovy/json/JsonGenerator.Options.html#addConverter-java.lang.Class-groovy.lang.Closure-]
 method.

It let's you do this for example:
{code:java}
def generator = new groovy.json.JsonGenerator.Options()
    .addConverter(Pojo) { Pojo p -> [test_two: p.testTwo, test_one: p.testOne] }
    .build()
{code}
Or a full example with a more general converter:
{code:java}
class Pojo {
  int testOne
  int testTwo
}

def generator = new groovy.json.JsonGenerator.Options()
  .addConverter(Pojo) { Pojo p -> p.properties
    .findAll{ it.key != 'class' }
    .collectEntries{ k, v -> [k.replaceAll(/([A-Z])/, /_$1/).toLowerCase(), v] 
} }
  .build()

def pojo = new Pojo(testOne: 1, testTwo: 1)

assert generator.toJson(pojo) == '{"test_two":1,"test_one":1}'
{code}


was (Author: paulk):
I'd recommend using the 
[JsonGenerator.Options.addConverter|http://docs.groovy-lang.org/latest/html/api/groovy/json/JsonGenerator.Options.html#addConverter-java.lang.Class-groovy.lang.Closure-]
 method.

It let's you do this for example:
{code:java}
class Pojo {
  int testOne
  int testTwo
}

def generator = new groovy.json.JsonGenerator.Options()
  .addConverter(Pojo) { Pojo p -> p.properties
    .findAll{ it.key != 'class' }
    .collectEntries{ k, v -> [k.replaceAll(/([A-Z])/, /_$1/).toLowerCase(), v] 
} }
  .build()

def pojo = new Pojo(testOne: 1, testTwo: 1)

assert generator.toJson(pojo) == '{"test_two":1,"test_one":1}'
{code}

> Provide a way to change property names when converting Pojo to JSON
> -------------------------------------------------------------------
>
>                 Key: GROOVY-8849
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8849
>             Project: Groovy
>          Issue Type: New Feature
>          Components: JSON
>    Affects Versions: 2.5.0
>            Reporter: Raviteja Lokineni
>            Priority: Critical
>
> I want to be able to override with something like the annotation JsonProperty 
> to override how a property name should be serialized to Json. If the feature 
> is already there then please add documentation. For example:
> {code:java}
> @JsonProperty("test_one")
> int testOne
> @JsonProperty("test_two")
> int testTwo{code}
> More information on the example: 
> [Jackson-Annotations|https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations]
>  
> h1. Reproducible Code
> h2. Pojo.groovy
> {code:java}
> class Pojo {
>   int testOne
>   int testTwo
> }{code}
> h2. Sample Run:
> {code:java}
> JsonOutput.toJson(new Pojo(testOne: 1, testTwo: 1))
> // Output
> // ===> {"testTwo":1,"testOne":1}
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to