GitHub user julianhowarth created a discussion: Simplest way to use Java's 
unmodifiable List classes

This is really basic but I'm missing something about the best way to 
serialize/deserialize a `List` of objects. The following shows what I'm hitting 
but I can't see an answer in the docs: 

```java
  @Test
  void foryLoadList() throws IOException {

    var fory = Fory.builder()
        .withLanguage(Language.JAVA)
        .withRefTracking(true)
        .requireClassRegistration(false)
        .build();

    var items = List.of("foo", "bar", "baz");

    try (var outputStream =
             new 
BufferedOutputStream(Files.newOutputStream(Path.of("output.fory")))) {
      outputStream.write(fory.serializeJavaObject(items));
    }

    try (var input = new 
BufferedInputStream(Files.newInputStream(Path.of("output.fory")))) {

      var res = (List<String>) fory.deserializeJavaObject(input.readAllBytes(), 
List.class);

      assertThat(res).isEqualTo(items);
    }
  }
```
It fails with:
```
org.apache.fory.exception.DeserializationException: Deserialize failed, read 
objects are: [null]

        at 
org.apache.fory.util.ExceptionUtils.handleReadFailed(ExceptionUtils.java:63)
        at org.apache.fory.Fory.deserializeJavaObject(Fory.java:1228)
        at org.apache.fory.Fory.deserializeJavaObject(Fory.java:1199)
        at 
com.endeavorstreaming.vesper.content_engine.loader.storage.EventSourcingForyTest.foryLoadList(EventSourcingForyTest.java:113)
        at java.base/java.lang.reflect.Method.invoke(Method.java:565)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
Caused by: java.lang.UnsupportedOperationException: Class interface 
java.util.List doesn't support serialization.
        at 
org.apache.fory.resolver.ClassResolver.getSerializerClass(ClassResolver.java:994)
        at 
org.apache.fory.resolver.ClassResolver.getSerializerClass(ClassResolver.java:988)
        at 
org.apache.fory.resolver.ClassResolver.createSerializer(ClassResolver.java:1377)
        at 
org.apache.fory.resolver.ClassResolver.getClassInfo(ClassResolver.java:1250)
        at org.apache.fory.Fory.deserializeJavaObject(Fory.java:1220)
        ... 5 more
``` 

using version `0.14.1`

In my actual use case the list is being generated from a `stream.map(...) ... 
.toList()` and we prefer immutable types so I can't just switch to an 
`ArrayList` for example. I've noticed that an array would work fine too , but 
I'd prefer not to have to incur the conversion/copy cost each time it is called.

Is there a simple solution to allow this to work?

GitHub link: https://github.com/apache/fory/discussions/3239

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to