Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more options to custom DefaultPrettyPrinter (example with grafana json dashboards) #1128

Closed
gulecroc opened this issue Oct 20, 2023 · 7 comments
Labels
2.17 Issues planned (at earliest) for 2.17

Comments

@gulecroc
Copy link
Contributor

Jackson version: 2.15.3

Hi, I worked on Grafana dashboards json files and need to adapt the DefaultPrettyPrinter.

I would like tu suggest some improvment to custom DefaultPrettyPrinter without the need to override some methods :

  1. Object key and value separator : remove space after object key
current  : "key" : "value"
expected : "key": "value"

I could not override this behavior, either with withSeparators or withoutSpacesInObjectEntries methods :

   // could not change spaces before or after separators
   public DefaultPrettyPrinter withSeparators(Separators separators) {
      this._separators = separators;
      this._objectFieldValueSeparatorWithSpaces = " " + separators.getObjectFieldValueSeparator() + " ";
      return this;
   }

   // disable spaces before and after
   public DefaultPrettyPrinter withoutSpacesInObjectEntries() {
      return this._withSpaces(false);
   }

   // need to override this method
   public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
      if (this._spacesInObjectEntries) {
         g.writeRaw(this._objectFieldValueSeparatorWithSpaces);
      } else {
         g.writeRaw(this._separators.getObjectFieldValueSeparator());
      }

   }

Need to override writeObjectFieldValueSeparator method :

    @Override
    public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
        g.writeRaw(": ");
    }

Maybe add new method withSeparators(String prefix, Separators separators, String suffix) ?

   public DefaultPrettyPrinter withSeparators(String prefix, Separators separators, String suffix) {
      this._separators = separators;
      this._objectFieldValueSeparatorWithSpaces = prefix + separators.getObjectFieldValueSeparator() + suffix;
      return this;
   }
  1. Array indenter : ident each array object
current :
{
  "array" : [ {
     [...]
   }, {
     [...]
   } ]
}

expected :
{
  "array" : [
     {
       [...]
     }, 
     {
       [...]
     } 
   ]
}

This could be done with :

DefaultPrettyPrinter pp = new DefaultPrettyPrinter ();
pp.withArrayIndenter(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE);

But it was not easy to find the good indenter, maybe add some documentation to indenters availables or add withArrayIndenterLineFeed method ?

  1. Empty object/array : remove space
current :

{
  "links": [ ]
  "custom": { }
}

expected :

{
  "links": []
  "custom": {}
}

I need to override writeEndObject and writeEndArray method to comment the behavior when nrOfValues = 0 :

  @Override
   public void writeEndObject(JsonGenerator g, int nrOfEntries) throws IOException {
      if (!this._objectIndenter.isInline()) {
         --this._nesting;
      }

      if (nrOfEntries > 0) {
         this._objectIndenter.writeIndentation(g, this._nesting);
      } 
      // else {
      //   g.writeRaw(' ');
      // }

      g.writeRaw('}');
   }

  @Override
   public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException {
      if (!this._arrayIndenter.isInline()) {
         --this._nesting;
      }

      if (nrOfValues > 0) {
         this._arrayIndenter.writeIndentation(g, this._nesting);
      } 
      // else {
      //    g.writeRaw(' ');
      // }

      g.writeRaw(']');
   }

Maybe add new methods withEmptyArrayContent(String content) ?

Finally, here is my custom class for those who could need it :

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.util.DefaultIndenter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;

public class GrafanaPrettyPrinter extends DefaultPrettyPrinter {

    @Override
    public DefaultPrettyPrinter createInstance() {
        GrafanaPrettyPrinter pp = new GrafanaPrettyPrinter();
        pp._arrayIndenter = DefaultIndenter.SYSTEM_LINEFEED_INSTANCE;
        return pp;
    }

    @Override
    public void writeObjectFieldValueSeparator(JsonGenerator g) throws IOException {
        g.writeRaw(": ");
    }

    @Override
    public void writeEndObject(JsonGenerator g, int nrOfEntries) throws IOException {
        if (!this._objectIndenter.isInline()) {
           --this._nesting;
        }
  
        if (nrOfEntries > 0) {
           this._objectIndenter.writeIndentation(g, this._nesting);
        }
  
        g.writeRaw('}');
     }

     @Override
     public void writeEndArray(JsonGenerator g, int nrOfValues) throws IOException {
        if (!this._arrayIndenter.isInline()) {
           --this._nesting;
        }
  
        if (nrOfValues > 0) {
           this._arrayIndenter.writeIndentation(g, this._nesting);
        }
  
        g.writeRaw(']');
     }
    
}
@cowtowncoder
Copy link
Member

With respect to entry (1) did you notice #1042 that is going in 2.16? (2.16.0-rc1 being released today)?

As to others, PRs are always welcome; feature requests themselves sound reasonable to me.

@cowtowncoder cowtowncoder added the 2.17 Issues planned (at earliest) for 2.17 label Oct 20, 2023
@gulecroc
Copy link
Contributor Author

Hi @cowtowncoder, thank's for the link #1042

I will open PRs for others features.

@gulecroc
Copy link
Contributor Author

gulecroc commented Dec 26, 2023

Update :

  1. Object key and value separator : configurable since v2.16
  2. Array indenter : N/A
  3. Empty object/array : PR Allow configuring DefaultPrettyPrinter separators for empty Arrays and Objects #1178 (merged in 2.17)

@erik-meuwese-topicus
Copy link

The extra whitespace in 2.16 breaks previous behavior. This should not be a minor change. When it's a minor change there should be a method on the defaultPrettyPrinter to enable the whitespace before the separator

@cowtowncoder
Copy link
Member

cowtowncoder commented Jan 12, 2024

@erik-meuwese-topicus All of these are options for user to change behavior, not changing default behavior, so I am not sure what extra space you are referring to?

Intent at least is not to make such changes, to preserve backwards-compatibility.

@cowtowncoder
Copy link
Member

@gulecroc Sounds like first and third entries have been implemented: should the second entry be moved to its own issue (create new one) and close this one?

@gulecroc
Copy link
Contributor Author

@cowtowncoder for me it sounds good. I close the issue. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.17 Issues planned (at earliest) for 2.17
Projects
None yet
Development

No branches or pull requests

3 participants