All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoft.serialization.serializers.json.UrlSerializer Maven / Gradle / Ivy

There is a newer version: 8.9.0
Show newest version
package com.cedarsoft.serialization.serializers.json;

import java.io.IOException;
import java.net.URL;

import com.cedarsoft.serialization.jackson.AbstractJacksonSerializer;
import com.cedarsoft.serialization.jackson.test.compatible.JacksonParserWrapper;
import com.cedarsoft.version.Version;
import com.cedarsoft.version.VersionException;
import com.cedarsoft.version.VersionRange;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonParser;
import org.codehaus.jackson.JsonProcessingException;

import javax.annotation.Nonnull;

public class UrlSerializer extends AbstractJacksonSerializer {
  public UrlSerializer() {
    super( "url", VersionRange.from( 1, 0, 0 ).to( 1, 0, 0 ) );
  }

  @Override
  public void serialize( @Nonnull JsonGenerator serializeTo, @Nonnull URL object, @Nonnull Version formatVersion ) throws IOException, JsonProcessingException {
    verifyVersionWritable( formatVersion );
    serializeTo.writeString( object.toString() );
  }

  @Nonnull
  @Override
  public URL deserialize( @Nonnull JsonParser deserializeFrom, @Nonnull Version formatVersion ) throws VersionException, IOException, JsonProcessingException {
    verifyVersionReadable( formatVersion );
    JacksonParserWrapper parser = new JacksonParserWrapper( deserializeFrom );

    final String text = parser.getText();
    return new URL( text );
  }

  @Override
  public boolean isObjectType() {
    return false;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy