logo
down
shadow

java/jackson - don't serialize wrapping class


java/jackson - don't serialize wrapping class

By : Li Wu
Date : November 15 2020, 06:54 AM
Hope that helps When serializing a list of string with Jackson library, it provides correctly a JSON array of strings: , You can use @JsonValue on your single getter
code :
@JsonValue
public String getS() {
    return s;
}


Share : facebook icon twitter icon
JAXB dont want wrapping class

JAXB dont want wrapping class


By : user76056
Date : March 29 2020, 07:55 AM
this one helps. You could use a StAX (JSR-173) XMLStreamReader to parse the XML document (an implementation is included in the JDK/JRE since Java SE 6). Then you could advance it to advance to each watermark element and then have JAXB (JSR-222) unmarshal that.
Demo
code :
import javax.xml.bind.*;
import javax.xml.stream.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Watermark.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();

        StreamSource source = new StreamSource("input.xml");
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(source);
        xsr.nextTag(); // Advance to "watermarks" element
        xsr.nextTag(); // Advance to "watermark" element

        while(xsr.getLocalName().equals("watermark")) {
             Watermark object = (Watermark) unmarshaller.unmarshal(xsr);
             System.out.println(object);
             xsr.nextTag();
        }
    }

}
How to serialize with Jackson a java.util.Map based class

How to serialize with Jackson a java.util.Map based class


By : user2059828
Date : March 29 2020, 07:55 AM
Hope that helps Most probably you will end up with implementing your own serializer which will handle your custom Map type. Please refer to this question for more information.
If you choose to replace inheritance with composition, that is to make your class to include a map field not to extend a map, then it is pretty easy to solve this using the @JsonAnyGetter annotation.
code :
public class JacksonMap {

    public static class Bean {
        private final String field;
        private final Map<String, Object> map;

        public Bean(String field, Map<String, Object> map) {
            this.field = field;
            this.map = map;
        }

        public String getField() {
            return field;
        }

        @JsonAnyGetter
        public Map<String, Object> getMap() {
            return map;
        }
    }

    public static void main(String[] args) throws JsonProcessingException {
        Bean map = new Bean("value1", Collections.<String, Object>singletonMap("key1", "value2"));
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(map));
    }
}
{"field":"value1","key1":"value2"}
JAVA JACKSON: serialize a class with two field instead of all class

JAVA JACKSON: serialize a class with two field instead of all class


By : DonDel
Date : March 29 2020, 07:55 AM
To fix this issue Solved adding a Json Serializer.
I have created an NationJsonSerializer for the parent class:
code :
public class NationJsonSerializer extends JsonSerializer<TNation> {

@Override
public void serialize(TNation value, JsonGenerator jgen, SerializerProvider provider) 
  throws IOException, JsonProcessingException {
    jgen.writeStartObject();
    jgen.writeNumberField("id", value.getId());
    jgen.writeStringField("name", value.getComune());
    jgen.writeStringField("iso", value.getCap());
    jgen.writeEndObject();
}
}
@JoinColumn(name = "idtnation",referencedColumnName = "id",nullable = true)
@ManyToOne(targetEntity = TNation.class, fetch = FetchType.EAGER)
@JsonSerialize(using = NationJsonSerializer.class)
private TNation nation;
Derive the exact class from the wrapping class Jackson

Derive the exact class from the wrapping class Jackson


By : Cptl
Date : March 29 2020, 07:55 AM
To fix the issue you can do Here is the solution for my problem, however the JsonTypeInfo.As.EXTERNAL_PROPERTY is since 1.9 version :(
Jackson JsonTypeInfo.As.EXTERNAL_PROPERTY doesn't work as expected
Java Jackson Serialize List of Abstract Class

Java Jackson Serialize List of Abstract Class


By : Georgina Gonzalez
Date : March 29 2020, 07:55 AM
To fix the issue you can do Scenario , I've found after a few days a solution:
code :
public static <T> Optional<String> toJson(final ObjectMapper mapper, final TypeReference<T> type, final T object) {
    try {
        return Optional.ofNullable(mapper.writer().forType(type).writeValueAsString(object));
    } catch (JsonProcessingException e) {
        return Optional.empty();
    }
}

public static void main(String[] args) {
    List<InfoItem> items = new ArrayList<>();
    // Set values in items...

    ObjectMapper mapper = new ObjectMapper();
    Optional<String> json = toJson(mapper, new TypeReference<List<InfoItem>>() {}, items);
    json.ifPresent(x -> System.out.println(x));
}
Related Posts Related Posts :
  • How to write generic Java API without method Overloading
  • Java Serialization / Deserialization of an ArrayList works only on first program execution
  • Conway's Game of Life's rules dont apply properly
  • call a java method when Click on a html button without using javascript
  • Why does SSLSocketFactory.createSocket returns a Socket instead of SSLSocket?
  • How to schedule a task in Tomcat
  • compare two pdf files (approach) using java
  • What is the use of allowCoreThreadTimeout( ) in ThreadPoolExecutor?
  • Describe the Strategy Design Pattern in Java?
  • MongoDB too many open connections
  • Issue with generate JAXB classes with value constructors
  • Moving from one frame to another in java swing
  • Get a thread by Id
  • how to open front camera and back camera at same time?
  • Messed up with Java Declaration
  • Extracting chart in EXcel as image using java
  • How to get latitude and longitude in Android phone?
  • Do GC pauses and kill -STOP produce the same behavior?
  • Android HTML.ImageGetter Images resized
  • The way to instantiate map<String, List<String>> in Java
  • Java Making objects move while buttons held
  • Androidx Navigation View - `setNavigationItemSelectedListener` Doesn't Work
  • after compiling code JFrame does not appear
  • Netbeans project, JFrames ran from the main JFrame close all JFrames in project
  • Square Retrofit Client: How to enable/disable followRedirects? How to intercept redirect URL?
  • Java Circular Reference - Can’t Compile
  • Mouse click on specific time JAVA
  • android 4 onCreateDialog return type incompatible when overriding
  • Java Implementation of Oracle's DBMS_UTILITY.GET_HASH_VALUE Function
  • javac cannot be run, and furthermore does not seem installed
  • Function to obtain a thread given its ID
  • Java File path to Executable has spaces
  • Minimum Oracle Java VM heap memory setting?
  • Elegant way to create one of a large number of classes
  • Java fx binding styles
  • Summing weights based on string in large file
  • Android ListView setOnItemClickListener not working
  • How is the Java Bootstrap Classloader loaded?
  • Check object is blank
  • Add image between text in JEditorPane
  • Java POI Excel creating new column and new rows
  • NullPointerException when managing bean with Spring+JSF(Primefaces)
  • Content autocomplete in spring tool suite 4
  • Super Noob Help ~ MouseEvent/Frame
  • Making one project with dependencies stand-alone in Eclipse (Java)
  • Jsoup- getting certain attributes from website
  • MongoDB, how to make a link between collections with JAVA code
  • Overriding an abstract method means class abstract class or not in java?
  • Bukkit teleport - nullPointerException
  • How to check if a Float variable is negative in java
  • Servlet: Cannot forward after response has been committed
  • sql query to select a specific cell in excel
  • How to make new picture when mouse dragged in java
  • Use Constants interface in GWT Project
  • Setting size of a JPanel in GroupLayout
  • Construct a map from two equal size arrays
  • Implementing Read-Write Locks with Double-Checked Locking
  • Disable NO_BUTTON from JOptionPane Java if a condition is true
  • UDP multiple socket
  • Android Threading: This Handler class should be static or leaks might occur
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org