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

Ryan Skraba commented on AVRO-3262:
-----------------------------------

i've always thought producing JSON from the {{toString()}} method was misguided 
(see AVRO-3061).  Unfortunately, I know there are developers that rely on the 
JSON representation from toString, and it's difficult to "deprecate" this usage 
of toString in a way that is meaningful to them. 

I would largely prefer the toString as you envision it (using the class name 
and contents), useful for debugging and logging.  The right way to get a 
well-specified and portable JSON representation of an object is to use a 
JSONEncoder.  Unfortunately the current toString() JSON is slightly different 
:( 

Do you think there's any way to "fix" this in a way that would give a better 
heads-up to the developers who would be affected by the change?  Another system 
property like *{{avro.tostring.includes.class}}*, either at runtime or when the 
record is generated, or an option that we could set when generating the code 
for the SpecificRecord ( *{{--toString-includes-class}}* )?

It might even be acceptable to provide the ability to override which base class 
is used (so that either "old" or custom logic can be provided).

> Include Avro record class name in toString
> ------------------------------------------
>
>                 Key: AVRO-3262
>                 URL: https://issues.apache.org/jira/browse/AVRO-3262
>             Project: Apache Avro
>          Issue Type: Improvement
>    Affects Versions: 1.11.0
>            Reporter: Dan Newton
>            Priority: Minor
>
> Avro objects output JSON when they are `toString`'d.
> This makes it difficult to understand what Avro objects are being logged when 
> passing the whole object into the logging string.
> It would be convenient if it contained the class name of the Avro object, so 
> that when used in strings or logging its easier to read.
> Note, this could be bypassed manually in user code by writing something like 
> `"logging ${avroObject::class.java.name} $avroObject"` (I'd write it in Java 
> but my head hurts right now).
> From what I looked at, possibly changing `SpecificRecordBase.toString` to the 
> following would resolve the issue:
>  
> {code:java}
> @Override
> public String toString() {
>   return this.class + " " + getSpecificData().toString(this);
> }{code}
>  
> Possibly the solution might be more elegant than that or in a slightly 
> different place.
> The solution above assumes that the `toString` isn't being used for anything 
> within the Avro code. If other code depends on the fact it outputs JSON then 
> what I've suggested won't be suitable.
> In that situation, we could add a new method that contains the class name 
> which can be called from user code and is still an improvement.
> On the flip side, the JSON only output could be moved and called from other 
> Avro code, leaving the class name `toString` as the default. This would be 
> preferred as Avro objects contained within other objects will be output with 
> their class names when turned into strings, which does not happen with the 
> previous option.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to