Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 982699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:44:19+00:00 2026-05-16T04:44:19+00:00

I know that you can mark a scala object as @serializable , but I

  • 0

I know that you can mark a scala object as @serializable, but I don’t understand what to do with the object afterwards. Do I simply treat it as a Java Serializable object?

I want to serialize the object into a stream of bytes. Can someone show me the code to transform a serialize object into either a byte array or a string?

(the googles have not been helpful with this question)

FOLLOWUP: Thanks. I now understand that I can use it like a Java Serializable object. Sometimes the obvious answer escapes me.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:44:19+00:00Added an answer on May 16, 2026 at 4:44 am

    To answer your first question: yes you can treat it as a Java Serializable object:

    
    scala> @serializable object A
    defined module A
    
    scala> import java.io.ByteArrayOutputStream;
    import java.io.ByteArrayOutputStream
    
    scala> import java.io.ObjectOutputStream;
    import java.io.ObjectOutputStream
    
    scala> val baos = new ByteArrayOutputStream(1024)
    baos: java.io.ByteArrayOutputStream = 
    
    scala> val o = new ObjectOutputStream(baos)
    o: java.io.ObjectOutputStream = java.io.ObjectOutputStream@3d689405
    
    scala> o.writeObject(A)
    
    scala> baos.toByteArray
    res4: Array[Byte] = Array(-84, -19, 0, 5, 115, 114, 0, 24, 108, 105, 110, 101, 49, 51, 36, 111, 98, 106, 101, 99, 116, 36, 36, 105, 119, 36, 36, 105, 119, 36, 65, 36, 110, -104, -28, -53, -123, -97, -118, -36, 2, 0, 0, 120, 112)
    
    scala> object B
    defined module B
    
    scala> o.writeObject(B)
    java.io.NotSerializableException: B$
        at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
        at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
        at .(:13)
        at .()
        at RequestResult$.(:9)
        at RequestResult$.()
        at RequestResult$scala_repl_result()
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
        at scala.tools.nsc.Interpreter$Request$...
    
    

    If you want to serialize to some string format, perhaps this library may be useful, which serializes scala objects into JSON.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that it's a subject that can raise a lot of debate, but
I want to know that can mouse click event detects colors or even strings.
I know that I can use cmp, diff, etc to compare two files, but
I know that I can iterate over an object's properties like this: for (property
I know that there can be multiple values for an email, but I'm not
I know that I can use regex to match substrings in a string, but
I know that can co-exists with web form no problem as Hanselman mention here
I know that one can define an 'expected' exception in JUnit, doing: @Test(expect=MyException.class) public
I know that I can get word completion through CTRL+N & CTRL+P and code
I know that this can be easily done by using if(i%5 == 0 OR

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.