runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Make System.Collections.Immutable [Serializable]

Open psmolinsky opened this issue 11 years ago • 32 comments

Please make collections from namespace System.Collections.Immutable serializable if it is possible - mark them with [Serializable] attribute and do other related changes. There can be many cases when this would be useful. Thanks.

psmolinsky avatar Mar 27 '15 13:03 psmolinsky

Binary serialization isn't available in the portable subset so [Serializable] isn't possible. At least to my knowledge.

jaredpar avatar Mar 27 '15 18:03 jaredpar

Correct @jaredpar the Serializable attribute isn't available in .NET Core because binary serialization isn't supported. @psmolinsky which serializer are you trying to use with Immutable collections?

weshaggard avatar Mar 28 '15 03:03 weshaggard

Tagging as "needs more info" to understand what serialization technologies @psmolinsky is using. As noted above, we don't have binary serialization in .NET Core.

ellismg avatar Mar 31 '15 22:03 ellismg

In my use case I'm trying to save some list of serializable objects to AppFabric cache. Serialization of generic List<> or array is fine. As far as immutable collections are mostly wrappers (ImmutableArray<> just wraps regular array), I don't understand why they also can't support serialization (be marked as [Serializable]). Thanks for clarification.

psmolinsky avatar Apr 10 '15 14:04 psmolinsky

+1 trying to store ImmutableList in CallContext.LogicalSetData, for which it needs to be [Serializable]

stevozilik avatar Jul 18 '15 14:07 stevozilik

[Serializable] attribute is not available in .Net Core. We discussed the possiblity of adding DataContract Surrogate in .Net Core (see https://github.com/dotnet/apireviews/pull/16) and @khdang is currently working on the feature(https://github.com/dotnet/corefx/issues/1931). When the surrogate type is available in .Net Core, it would be up to the owner of the System.Collections.Immutable Namespace to decide whether to support serialization of Immutable types using surrogates.

Assigning the issue back to @ellismg and removing the serializaiton label.

shmao avatar Oct 16 '15 20:10 shmao

SerializableAttribute is now available in .NET Core. @psmolinsky Can you come up with the PR for these changes and a proposal to be approved in api-review.

Priya91 avatar Sep 28 '16 21:09 Priya91

@psmolinsky do you have interest in writing up the quick formal proposal here? (API is trivial, but with motivations)

See API review process

danmoseley avatar Dec 27 '16 18:12 danmoseley

Marking as up for grabs to see if there is some interest in doing the formal API proposal, if there is none we will just close it.

safern avatar Mar 06 '17 19:03 safern

I think our current stance is that we don't want to add [Serializable] to any of our types if we don't absolutely have to, because of the huge compatibility burden it adds. @terrajobst , I think you have strong opinions about this?

mellinoe avatar Mar 06 '17 20:03 mellinoe

Also adding @nguerrera , @AArnott

mellinoe avatar Mar 06 '17 20:03 mellinoe

I agree: it's preferable to keep serialization in the business of a serialization library rather than offering it in the type being serialized. Newtonsoft.Json and another popular super-compact-and-efficient serialization library (whose name escapes me at the moment) both support immutable collections natively. For anyone currently asking for this, why do they need the support in the framework as opposed to using a 3rd party serialization library?

AArnott avatar Mar 06 '17 20:03 AArnott

I agree with you it's better to use NewtonSoft.Json or Protobuf. And for new project that's what I'll do. But in my team we have a lot of legacy binary serialization, so actually we can't use easily immutable collection in our legacy structures. Make immutable collection serializable will help developers to use it in legacy project.

fabricefou avatar Mar 11 '17 08:03 fabricefou

I'm in the same boat as @fabricefou - in my case a Web Forms app that stores a lot of custom objects in ViewState. Migrating many of these objects to use immutable instead of mutable collections should be a no-brainer, but the lack of serializability makes it impossible without complicated workarounds.

sab39 avatar Mar 28 '17 17:03 sab39

I've implemented the ISerializationSurrogateProvider interface for the most common System.Collection.Immutable types. It works pretty good with DataContractSerializer now. Please take a look here: https://github.com/bp74/System.Collections.Immutable.Surrogate

bp74 avatar Aug 12 '17 10:08 bp74

Please make sure that whatever serialization logic gets implemented (if any) does not produce results that expose the internal structure of these collections. Several of the collections currently use AVL trees, and we don't want serialization concerns to block progress, e.g. dotnet/corefx#1502.

Exposing the internals of these to serialization would be roughly equivalent to marking them [Obsolete]. We'd end up with a slow, legacy set of collection instances and a new set using more efficient structures.

sharwell avatar Aug 12 '17 20:08 sharwell

@sharwell It is serialized like a standard List or Dictionary. If you look at the XML you won't see anything from the internal structure.

bp74 avatar Aug 13 '17 07:08 bp74

@bp74 I just published a very similar library. Main difference is that you don't have to configure the serializer (which sometimes isn't possible). And it would work with any serializer that supports standard .NET collections.

aelij avatar Aug 29 '17 08:08 aelij

[Serializable] Immutable* would be great.

At the moment I've got the following dirty workaround, e.g. with ImmutableDictionary<,>

[Serializable]
public class MyClass
{
    [NonSerialized]
    private IImmutableDictionary<string, int> dictionary;

    private readonly IDictionary<string, int> backingDictionary; // This is what gets serialized

    public MyClass()
    {
        backingDictionary = new Dictionary<string, int>(/* whatever */);
    }

    public IImmutableDictionary<string, int> Dictionary => dictionary ?? (dictionary = backingDictionary.ToImmutableDictionary());
}

This works by having an internal state which is [Serializable], and then lazy creates the Immutable. Works when you don't have any control over the IFormatter to notify it of your ISerializationSurrogates.

Kinda sucks though, because it's storing two collections (so ~2x number of pointers I'd guess?), but it's "good enough for me"

A better way would be to do your own manual serialization, then, for example, set the backing field to null so it can be GC'd. However if you are targeting netstandard2.0, and someone wants to use your lib in netframework, you can't because [OnDeserialized] etc... attributes don't work yet (https://github.com/dotnet/standard/issues/300)

Mardoxx avatar Aug 30 '17 10:08 Mardoxx

Any news on this issue?

fabricefou avatar Oct 03 '18 02:10 fabricefou