pascallouisperez / jsonmarshaller

JsonMarshaller is a Java 1.5 library that allows marshalling and unmarshalling of JSON objects to and from entities ("Java classes").
Apache License 2.0
1 stars 0 forks source link

Map<Entity, Entity> and Map<Entity, String > should be marshallable. #30

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Entity Class :
@Entity
public class EntityMap {
  @Value
  private final Map<Email, String> emails = new HashMap<Email, String>();

  public void addEmail(String name, Email email) {
    emails.put(email, name);
  }

  public int numberOfEmails() {
    return emails.size();
  }

  public String get(String name) {
    return "";//emails.get(name);
  }

  public Map<Email, String> getEmails() {
    return emails;
  }
}

What is the expected output? What do you see instead?

Please use labels and text to provide additional information.

Exception: 
java.lang.IllegalArgumentException: Map<String, ...> must be used. 
Signature Ljava/util/Map<Lcom/twolattes/json/Email;Ljava/lang/String;>;
    at com.twolattes.json.EntitySignatureVisitor.getDescriptor
(EntitySignatureVisitor.java:205)
    at com.twolattes.json.DescriptorFactory.create
(DescriptorFactory.java:162)
    at com.twolattes.json.EntityFieldVisitor.visitEnd
(EntityFieldVisitor.java:72)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at org.objectweb.asm.ClassReader.accept(Unknown Source)
    at 
com.twolattes.json.DescriptorFactory.createConcreteEntityDescriptor
(DescriptorFactory.java:117)
    at com.twolattes.json.DescriptorFactory.create
(DescriptorFactory.java:82)
    at com.twolattes.json.DescriptorFactoryTest.create
(DescriptorFactoryTest.java:231)
    at com.twolattes.json.DescriptorFactoryTest.testMap
(DescriptorFactoryTest.java:176)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
    at org.junit.internal.runners.TestMethodRunner.executeMethodBody
(TestMethodRunner.java:99)
    at org.junit.internal.runners.TestMethodRunner.runUnprotected
(TestMethodRunner.java:81)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected
(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestMethodRunner.runMethod
(TestMethodRunner.java:75)
    at org.junit.internal.runners.TestMethodRunner.run
(TestMethodRunner.java:45)
    at 
org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod
(TestClassMethodsRunner.java:66)
    at org.junit.internal.runners.TestClassMethodsRunner.run
(TestClassMethodsRunner.java:35)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected
(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected
(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run
(TestClassRunner.java:52)
    at org.junit.internal.runners.CompositeRunner.run
(CompositeRunner.java:29)
    at org.junit.internal.runners.TestClassRunner$1.runUnprotected
(TestClassRunner.java:42)
    at org.junit.internal.runners.BeforeAndAfterRunner.runProtected
(BeforeAndAfterRunner.java:34)
    at org.junit.internal.runners.TestClassRunner.run
(TestClassRunner.java:52)
    at com.intellij.rt.junit4.Junit4ClassSuite.run
(Junit4ClassSuite.java:99)
    at com.intellij.rt.execution.junit.JUnitStarter.main
(JUnitStarter.java:40)

Note:
In EntitySignatureVisitor.java, it is hardcoded that all the keys in a map 
must be of type String. 

It should allow a key of any type, as in this case it of type email which 
is a valid key. 

If there is a restriction that you can not provide this functionality, 
than you can remove this check and use toString() of the object to get the 
string represenation of the String. 

Original issue reported on code.google.com by nabil.sh...@gmail.com on 18 Jan 2010 at 9:53

GoogleCodeExporter commented 9 years ago
I have made few changes in the attached files and now it seems to work with all 
the 
types. 

Original comment by nabil.sh...@gmail.com on 18 Jan 2010 at 3:21

Attachments:

GoogleCodeExporter commented 9 years ago
Given a Map<K, V> the marshaller should work the following way:
K is either a String, a *value* type which converts to Json.String or an inlined
entity whose only field converts to Json.String. For values or entities which 
convert
to Json.Number, an automatic coercien to Json.String would be assumed.
V has no restriction, value or entity which can be marshalled.

The JsonMarshaller being test driven, we never accept any patch which does not 
full
test coverage.

Original comment by pascallo...@gmail.com on 18 Jan 2010 at 4:05

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
But what is the reason of restricting keys of type object. In real World, the 
key 
could be any thing.

Original comment by nabil.sh...@gmail.com on 19 Jan 2010 at 10:05

GoogleCodeExporter commented 9 years ago
Also tell me from where can I get the testdata/sample%s_pretty.json, as it is 
not 
included in the source code. 

All the test cases are successful and except the regression. 

Original comment by nabil.sh...@gmail.com on 19 Jan 2010 at 11:03

Attachments:

GoogleCodeExporter commented 9 years ago
Keys must be strings in JSON. Please read the spec at www.json.org

Having the existing pass is obviously required, but not sufficient. You must 
have
NEW, ADDITIONAL tests that clearly define and test the WHOLE feature.

Original comment by pascallo...@gmail.com on 20 Jan 2010 at 4:01