mleap icon indicating copy to clipboard operation
mleap copied to clipboard

Serialize mleap defaultleapframe to json

Open sumitjha4321 opened this issue 5 years ago • 2 comments

I am trying to serialize mleap's DefaultLeapFrame to json format. The doc shows the following example:

val schema = StructType(StructField("features", TensorType(BasicType.Double)),
  StructField("name", ScalarType.String),
  StructField("list_data", ListType(BasicType.String)),
  StructField("nullable_double", ScalarType(BasicType.Double, true)),
  StructField("float", ScalarType.Float),
  StructField("byte_tensor", TensorType(BasicType.Byte)),
  StructField("short_list", ListType(BasicType.Short)),
  StructField("nullable_string", ScalarType(BasicType.String, true))).get
val dataset = Seq(Row(Tensor.denseVector(Array(20.0, 10.0, 5.0)),
  "hello", Seq("hello", "there"),
  Option(56.7d), 32.4f,
  Tensor.denseVector(Array[Byte](1, 2, 3, 4)),
  Seq[Short](99, 12, 45),
  None))

val frame = DefaultLeapFrame(schema, dataset)

// Store Leap Frame
for(bytes <- frame.writer("ml.combust.mleap.json").toBytes();
    frame2 <- FrameReader("ml.combust.mleap.json").fromBytes(bytes)) {
  println(new String(bytes)) // print the JSON bytes
  assert(frame == frame2)
}

But locally I don't find the method writer for the DefaultLeapFrame object. I checked the source code for DefaultLeapFrame and LeapFrame as well but couldn't find the writer method there as well, as mentioned in the doc example.

Is it some implicit import or something ? Please do let know.
Thanks in advance.

sumitjha4321 avatar Sep 16 '20 02:09 sumitjha4321

writer method is in the DefaultLeapFrame class, you must be looking somewhere else

/** Writer for this leap frame * * @param format package with a DefaultWriter * @param ct class tag of this leap frame * @return writer for this leap frame with specified format */ def writer(format: String = BuiltinFormats.json) (implicit ct: ClassTag[LF]): FrameWriter = FrameWriter(lf, format)

mtsol avatar Oct 27 '20 09:10 mtsol

Hi @mtsol I didn't see writer method in any of DefaultLeapFrame, LeapFrame, FrameBuilder .

Also, when I run the example code in doc, I got error: value writer is not a member of ml.combust.mleap.runtime.frame.DefaultLeapFrame for(bytes <- frame.writer("ml.combust.mleap.json").toBytes();

Modify the code from

...
// Store Leap Frame
for(bytes <- frame.writer("ml.combust.mleap.json").toBytes(); // error
...

to

...
// Store Leap Frame
for(bytes <- FrameWriter(frame).toBytes();
...

This works for me.

johnnyasd12 avatar Dec 17 '21 06:12 johnnyasd12