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

Ryan Skraba commented on AVRO-2723:
-----------------------------------

Hello!  I'm not sure I understand your use case!

Typically when you use reflection in Java to serialize/deserialize POJO 
objects, you have enough information about the interesting POJO right at your 
hands.  There's no particular need to look for the default in the generated 
schema, if you have access to a *{{User}}* instance, right?

If you have a POJO instance and you want to get the default for a field, you 
can use plain Java: *{{new User().first}}*

If you have control over the POJO code and want to set the default in the 
generated schema, you can add an *{{@AvroDefault}}* annotation to the field 
[like this unit 
test|https://github.com/apache/avro/blob/70260919426f89825ca148f5ee815f3b2cf4764d/lang/java/avro/src/test/java/org/apache/avro/reflect/TestReflect.java#L1262].
  Is this what you are looking for?



> Avro Java: Obtaining default values for POJO objects with ReflectData
> ---------------------------------------------------------------------
>
>                 Key: AVRO-2723
>                 URL: https://issues.apache.org/jira/browse/AVRO-2723
>             Project: Apache Avro
>          Issue Type: Wish
>          Components: java
>    Affects Versions: 1.9.1
>            Reporter: Anh Le
>            Priority: Critical
>
> Hi guys,
>  
> I've got a simple app using Avro Reflection:
>  
> {code:java}
> public class App {
>   public static void main(String[] args) {
>     testReflection();
>   }
>   static class User {
>     public String first = "Andy";
>     public String last = "Le";
>   }
>   static void testReflection(){
>     // get the reflected schema for packets
>     Schema schema = ReflectData.AllowNull.get().getSchema(User.class);
>     System.out.println(schema.toString(true));
>   }
> {code}
> The output on console will be:
> {noformat}
> {
>   "type" : "record",
>   "name" : "User",
>   "namespace" : "App",
>   "fields" : [ {
>     "name" : "first",
>     "type" : [ "null", "string" ],
>     "default" : null
>   }, {
>     "name" : "last",
>     "type" : [ "null", "string" ],
>     "default" : null
>   } ]
> }
> {noformat}
>  
> As you can see, there's no default values for fields. Would you please tell 
> me how to obtain such values?
> Thank you.
>  
>  
>  
>  
>  



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

Reply via email to