How to display decimal in specific format in Jackson (for JSON)












0















Suppose I have an object with



private Double test;

// Need specific output in JSON via Jackson: test = 24.6000


When output to JSON via Jackson, I get 24.6, but I need the exact 4-decimal output as in the example. Does Jackson allow this?



For example, for Dates, we found a way to force MM/dd/yyyy:



@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
Date myDate;


We need something similar for Decimal formatting.










share|improve this question





























    0















    Suppose I have an object with



    private Double test;

    // Need specific output in JSON via Jackson: test = 24.6000


    When output to JSON via Jackson, I get 24.6, but I need the exact 4-decimal output as in the example. Does Jackson allow this?



    For example, for Dates, we found a way to force MM/dd/yyyy:



    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
    Date myDate;


    We need something similar for Decimal formatting.










    share|improve this question



























      0












      0








      0








      Suppose I have an object with



      private Double test;

      // Need specific output in JSON via Jackson: test = 24.6000


      When output to JSON via Jackson, I get 24.6, but I need the exact 4-decimal output as in the example. Does Jackson allow this?



      For example, for Dates, we found a way to force MM/dd/yyyy:



      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
      Date myDate;


      We need something similar for Decimal formatting.










      share|improve this question
















      Suppose I have an object with



      private Double test;

      // Need specific output in JSON via Jackson: test = 24.6000


      When output to JSON via Jackson, I get 24.6, but I need the exact 4-decimal output as in the example. Does Jackson allow this?



      For example, for Dates, we found a way to force MM/dd/yyyy:



      @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM/dd/yyyy")
      Date myDate;


      We need something similar for Decimal formatting.







      json spring jackson






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 15:58







      gene b.

















      asked Nov 13 '18 at 15:50









      gene b.gene b.

      1,77762554




      1,77762554
























          1 Answer
          1






          active

          oldest

          votes


















          1














          One way of doing this is to use custom json serializer and specify in @JsonSerialize.



          @JsonSerialize(using = CustomDoubleSerializer.class)
          public Double getAmount()

          public class CustomDoubleSerializer extends JsonSerializer<Double> {
          @Override
          public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
          if (null == value) {
          jgen.writeNull();
          } else {
          final String pattern = ".####";
          final DecimalFormat myFormatter = new DecimalFormat(pattern);
          final String output = myFormatter.format(value);
          jgen.writeNumber(output);
          }
          }
          }





          share|improve this answer


























          • thanks so there is no out-of-the-box Jackson annotation, in other words? For most cases there are.

            – gene b.
            Nov 13 '18 at 16:17






          • 1





            I don't think there is. But it would be a nice addition since this is a very common use case.

            – shakhawat
            Nov 14 '18 at 4:17











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284677%2fhow-to-display-decimal-in-specific-format-in-jackson-for-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          One way of doing this is to use custom json serializer and specify in @JsonSerialize.



          @JsonSerialize(using = CustomDoubleSerializer.class)
          public Double getAmount()

          public class CustomDoubleSerializer extends JsonSerializer<Double> {
          @Override
          public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
          if (null == value) {
          jgen.writeNull();
          } else {
          final String pattern = ".####";
          final DecimalFormat myFormatter = new DecimalFormat(pattern);
          final String output = myFormatter.format(value);
          jgen.writeNumber(output);
          }
          }
          }





          share|improve this answer


























          • thanks so there is no out-of-the-box Jackson annotation, in other words? For most cases there are.

            – gene b.
            Nov 13 '18 at 16:17






          • 1





            I don't think there is. But it would be a nice addition since this is a very common use case.

            – shakhawat
            Nov 14 '18 at 4:17
















          1














          One way of doing this is to use custom json serializer and specify in @JsonSerialize.



          @JsonSerialize(using = CustomDoubleSerializer.class)
          public Double getAmount()

          public class CustomDoubleSerializer extends JsonSerializer<Double> {
          @Override
          public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
          if (null == value) {
          jgen.writeNull();
          } else {
          final String pattern = ".####";
          final DecimalFormat myFormatter = new DecimalFormat(pattern);
          final String output = myFormatter.format(value);
          jgen.writeNumber(output);
          }
          }
          }





          share|improve this answer


























          • thanks so there is no out-of-the-box Jackson annotation, in other words? For most cases there are.

            – gene b.
            Nov 13 '18 at 16:17






          • 1





            I don't think there is. But it would be a nice addition since this is a very common use case.

            – shakhawat
            Nov 14 '18 at 4:17














          1












          1








          1







          One way of doing this is to use custom json serializer and specify in @JsonSerialize.



          @JsonSerialize(using = CustomDoubleSerializer.class)
          public Double getAmount()

          public class CustomDoubleSerializer extends JsonSerializer<Double> {
          @Override
          public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
          if (null == value) {
          jgen.writeNull();
          } else {
          final String pattern = ".####";
          final DecimalFormat myFormatter = new DecimalFormat(pattern);
          final String output = myFormatter.format(value);
          jgen.writeNumber(output);
          }
          }
          }





          share|improve this answer















          One way of doing this is to use custom json serializer and specify in @JsonSerialize.



          @JsonSerialize(using = CustomDoubleSerializer.class)
          public Double getAmount()

          public class CustomDoubleSerializer extends JsonSerializer<Double> {
          @Override
          public void serialize(Double value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException {
          if (null == value) {
          jgen.writeNull();
          } else {
          final String pattern = ".####";
          final DecimalFormat myFormatter = new DecimalFormat(pattern);
          final String output = myFormatter.format(value);
          jgen.writeNumber(output);
          }
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 16:11

























          answered Nov 13 '18 at 16:01









          shakhawatshakhawat

          1,6151026




          1,6151026













          • thanks so there is no out-of-the-box Jackson annotation, in other words? For most cases there are.

            – gene b.
            Nov 13 '18 at 16:17






          • 1





            I don't think there is. But it would be a nice addition since this is a very common use case.

            – shakhawat
            Nov 14 '18 at 4:17



















          • thanks so there is no out-of-the-box Jackson annotation, in other words? For most cases there are.

            – gene b.
            Nov 13 '18 at 16:17






          • 1





            I don't think there is. But it would be a nice addition since this is a very common use case.

            – shakhawat
            Nov 14 '18 at 4:17

















          thanks so there is no out-of-the-box Jackson annotation, in other words? For most cases there are.

          – gene b.
          Nov 13 '18 at 16:17





          thanks so there is no out-of-the-box Jackson annotation, in other words? For most cases there are.

          – gene b.
          Nov 13 '18 at 16:17




          1




          1





          I don't think there is. But it would be a nice addition since this is a very common use case.

          – shakhawat
          Nov 14 '18 at 4:17





          I don't think there is. But it would be a nice addition since this is a very common use case.

          – shakhawat
          Nov 14 '18 at 4:17


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284677%2fhow-to-display-decimal-in-specific-format-in-jackson-for-json%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ