Home > Blockchain >  How to serialise/deserialise an object in Scala?
How to serialise/deserialise an object in Scala?

Time:12-14

I am new to Scala/Java, and I need to see (and then maybe modify as per required) the transport options of default BQ instance

This is the code so far which seems to be printing the object pointer, instead of the value.

transportOptions is of class "TransportOptions" which extends "Serializable"

val bqservice = BigQueryOptions.getDefaultInstance
val transportOptions = bqservice.getTransportOptions
println(transportOptions)

Output :

com.google.cloud.spark.bigquery.repackaged.com.google.cloud.http.HttpTransportOptions@d3e83eb0

How to check the values inside this object? Thanks!

CodePudding user response:

In your example TransportOptions is an interface and the following class HttpTransportOptions implements this interface :

public class HttpTransportOptions implements TransportOptions {
    private static final long serialVersionUID = 7890117765045419810L;
    private final int connectTimeout;
    private final int readTimeout;
    private final String httpTransportFactoryClassName;
    private transient HttpTransportFactory httpTransportFactory;

For example, you can write an unit test and check the values inside with a breakpoint in your code, example with IntelliJ Idea and Kotlin (the principle is the same with Java and Scala) :

  @Test
  fun yourTest() {
      val bqservice = BigQueryOptions.getDefaultInstance()
      val transportOptions = bqservice.transportOptions
      println(transportOptions)
  }

enter image description here

CodePudding user response:

This is the code so far which seems to be printing the object pointer, instead of the value.

In Scala (like Java) there are no pointers (unless you're using Scala Native), just values of reference types are the references.

Is Java "pass-by-reference" or "pass-by-value"?

The println output means that .toString wasn't overridden and produces class name and instance hash code.

transportOptions has type com.google.cloud.TransportOptions. It is an interface and is implemented by class com.google.cloud.http.HttpTransportOptions.

For example you can find out field values with reflection

// val bqservice = BigQueryOptions.getDefaultInstance
val bqservice = BigQueryOptions.newBuilder()
  .setProjectId("XXX") // (*)
  .build
val transportOptions = bqservice.getTransportOptions

classOf[TransportOptions].getFields
  .foreach(println)

classOf[TransportOptions].getDeclaredFields
  .foreach(println)

classOf[HttpTransportOptions].getFields
  .map(field => field.getName -> field.get(transportOptions))
  .foreach(println)

classOf[HttpTransportOptions].getDeclaredFields
  .map(field => {
    field.setAccessible(true)
    field.getName -> field.get(transportOptions)
   })
  .foreach(println)
//(serialVersionUID,7890117765045419810)
//(connectTimeout,-1)
//(readTimeout,-1)
//(httpTransportFactoryClassName,com.google.cloud.http.HttpTransportOptions$DefaultHttpTransportFactory)
//(httpTransportFactory,com.google.cloud.http.HttpTransportOptions$DefaultHttpTransportFactory@6e0dec4a)

You can repeat with httpTransportFactory.

getFields returns fields of current class and its parents but only public ones, getDeclaredFields returns fields of only current class but both public and private.

(*) How to fix error: A project ID is required for this service but could not be determined

  • Related