[ 
https://issues.apache.org/jira/browse/AVRO-2775?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17258991#comment-17258991
 ] 

Andy Le commented on AVRO-2775:
-------------------------------

[~rskraba] My use case is: I want to extract default values for fields. Please 
see my story here: [https://github.com/anhldbk/avro-fork]

 

For the time being, the code below throws exceptions:

 
{code:java}

import org.apache.avro.Schema;
import org.apache.avro.reflect.ReflectData;

public class AvroPlay {
  public static class Age{
    public int value = 9;
  }

  public static class Person{
    public String name = "Andy";
    public Age age = new Age();
  }

  public static void main(String[] args) {
    ReflectData reflector = new ReflectData().setDefaultsGenerated(true);
    Schema schema = reflector.getSchema(Person.class);
    System.out.println(schema.toString(true));
  }
}

{code}

When I run it, I've got an exception of:

{noformat}
Exception in thread "main" org.apache.avro.AvroRuntimeException: Unknown datum 
class: class org.example.AvroPlay$Age
        at 
org.apache.avro.util.internal.JacksonUtils.toJson(JacksonUtils.java:96)
        at 
org.apache.avro.util.internal.JacksonUtils.toJsonNode(JacksonUtils.java:53)
        at org.apache.avro.Schema$Field.<init>(Schema.java:581)
        at 
org.apache.avro.reflect.ReflectData.createSchema(ReflectData.java:747)
        at 
org.apache.avro.specific.SpecificData$3.computeValue(SpecificData.java:328)
        at 
org.apache.avro.specific.SpecificData$3.computeValue(SpecificData.java:325)
        at java.lang.ClassValue.getFromHashMap(ClassValue.java:227)
        at java.lang.ClassValue.getFromBackup(ClassValue.java:209)
        at java.lang.ClassValue.get(ClassValue.java:115)
        at 
org.apache.avro.specific.SpecificData.getSchema(SpecificData.java:339)
        at org.example.AvroPlay.main(AvroPlay.java:18)
{noformat}

So is it reasonable to modify the behavior of 
`JacksonUtils.toJson(JacksonUtils.java:96)` a little bit?
 

 

> JacksonUtils: exception when calling toJsonNode() 
> --------------------------------------------------
>
>                 Key: AVRO-2775
>                 URL: https://issues.apache.org/jira/browse/AVRO-2775
>             Project: Apache Avro
>          Issue Type: Bug
>          Components: java
>    Affects Versions: 1.9.2
>            Reporter: Andy Le
>            Priority: Major
>
> I've got a simple test as followed
> {code:java}
> public class TestJacksonUtils {
>   public static class Age{
>     public int value = 9;
>   }
>   @Test
>   public void testToJson(){
>     Map<String, Object> kv = new HashMap<>();
>     kv.put("age", 9);
>     JsonNode node1 = JacksonUtils.toJsonNode(kv); // -> This is OK
>     Object obj = new Age();
>     JsonNode node2 = JacksonUtils.toJsonNode(obj); // -> This will trigger an 
> exception
>   }
> }
> {code}
> When I ran the test:
> {noformat}
> org.apache.avro.AvroRuntimeException: Unknown datum class: class 
> org.apache.avro.util.internal.TestJacksonUtils$Age
>       at 
> org.apache.avro.util.internal.JacksonUtils.toJson(JacksonUtils.java:87)
>       at 
> org.apache.avro.util.internal.JacksonUtils.toJsonNode(JacksonUtils.java:48)
>       at 
> org.apache.avro.util.internal.TestJacksonUtils.testToJson(TestJacksonUtils.java:20)
> {noformat}
> I've read the code & tests for JacksonUtils. Instead of raising exceptions at 
> [line 
> #87|https://github.com/apache/avro/blob/master/lang/java/avro/src/main/java/org/apache/avro/util/internal/JacksonUtils.java#L87],
>  I see we can auto convert objects into maps, every thing's gonna fine.
> My question is:
> - Is raising exception acceptable?
> - Any other way to have `toJsonNode` for general objects?



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to