Skip to content

Commit

Permalink
Fixed #44
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 5, 2013
1 parent a2a769a commit 1103e02
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.hibernate.collection.PersistentCollection;

import com.fasterxml.jackson.core.*;

import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import com.fasterxml.jackson.databind.ser.*;
Expand Down Expand Up @@ -56,14 +55,15 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer<?> ser = provider.handleContextualization(_serializer, property);

// If we use eager loading, or force it, can just return underlying serializer as is
if (_forceLazyLoading || !usesLazyLoading(property)) {
if (_serializer instanceof ContextualSerializer) {
return ((ContextualSerializer) _serializer).createContextual(provider, property);
}
return _serializer;
return ser;
}
if (ser != _serializer) {
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
}
// Otherwise this instance is to be used
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.datatype.hibernate3;

import java.util.*;

import javax.persistence.OneToMany;

import com.fasterxml.jackson.databind.ObjectMapper;

public class OneToManyTest extends BaseTest
{
static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";

static final class X {
@OneToMany
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

static final class Y {
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

public void testMap() throws Exception {
Y object = new Y();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

public void testMapWithOneToMany() throws Exception {
X object = new X();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

private String mapWithHibernateModule(Object object) throws Exception {
return new ObjectMapper().registerModule(new Hibernate3Module()).writeValueAsString(object);
}

private String mapWithoutHibernateModule(Object object) throws Exception {
return new ObjectMapper().writeValueAsString(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TestVersions extends BaseTest
* Not a good to do this, but has to do, for now...
*/
private final static int MAJOR_VERSION = 2;
private final static int MINOR_VERSION = 2;
private final static int MINOR_VERSION = 3;

// could inject using Maven filters as well...
private final static String GROUP_ID = "com.fasterxml.jackson.datatype";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
BeanProperty property)
throws JsonMappingException
{
JsonSerializer<?> ser = provider.handleContextualization(_serializer, property);

// If we use eager loading, or force it, can just return underlying serializer as is
if (_forceLazyLoading || !usesLazyLoading(property)) {
if (_serializer instanceof ContextualSerializer) {
return ((ContextualSerializer) _serializer).createContextual(provider, property);
}
return _serializer;
return ser;
}
if (ser != _serializer) {
return new PersistentCollectionSerializer(_forceLazyLoading, ser);
}
// Otherwise this instance is to be used
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.fasterxml.jackson.datatype.hibernate4;

import java.util.*;

import javax.persistence.OneToMany;

import com.fasterxml.jackson.databind.ObjectMapper;

public class OneToManyTest extends BaseTest
{
static final String EXPECTED_JSON = "{\"m\":{\"A\":\"A\"}}";

static final class X {
@OneToMany
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

static final class Y {
public final Map<String, String> m = new LinkedHashMap<String, String>();
}

public void testMap() throws Exception {
Y object = new Y();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

public void testMapWithOneToMany() throws Exception {
X object = new X();
object.m.put("A", "A");

assertEquals(EXPECTED_JSON, mapWithoutHibernateModule(object));
assertEquals(EXPECTED_JSON, mapWithHibernateModule(object));
}

private String mapWithHibernateModule(Object object) throws Exception {
return new ObjectMapper().registerModule(new Hibernate4Module()).writeValueAsString(object);
}

private String mapWithoutHibernateModule(Object object) throws Exception {
return new ObjectMapper().writeValueAsString(object);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public void testMapperVersions()
assertVersion(new Hibernate4Module());
}

/*
/**********************************************************
/* Helper methods
/**********************************************************
*/
private void assertVersion(Versioned vers)
{
Version v = vers.version();
Expand Down

0 comments on commit 1103e02

Please sign in to comment.