0

I am using CloudNative.CloudEvents nuget package from https://cloudevents.io/ for cloudevents issues.

I am getting the below error in my extension for binary serializer by using SerializeToUtf8Bytes() method.

Serialization and deserialization of 'System.Type' instances are not supported. Path: $.SpecVersion.IdAttribute.Type.ClrType.

  public static bool IsEventBatchReachedLimits<T>(this IEnumerable<T> list)
  {
     var options = new JsonSerializerOptions() { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) };

     var resultBytes = JsonSerializer.SerializeToUtf8Bytes(list, options);
     string[] sizes = { "B", "KB", "MB", "GB", "TB" };
        var len = Convert.ToDouble(resultBytes.Length);
        var order = 0;
        while (len >= 1024D && order < sizes.Length - 1)
        {
           order++;
           len /= 1024;
        }
        return (sizes[order] == "MB" && len >= 1) ? true : false;
     
  }

Can you tell me the reason why I can not serialize List to utf8 Bytes ?

Usage above extension: Type of cloudEvents list is List

enter image description here :

Actually, my real question is How can I learn my list size as MB or KB?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Penguen
  • 16,836
  • 42
  • 130
  • 205
  • 1
    What are you passing to your method? – phuzi Aug 25 '22 at 15:27
  • 1
    I would argue that provided error is quite self-explanatory. The data you are trying to serialize contains instance of `System.Type` which can't be serialized. – Guru Stron Aug 25 '22 at 15:34
  • 1
    Okay, so what would the type `T` be in this invocation. I'm guessing that one of `T`'s properties is of type `System.Type` which cannot be serialised – phuzi Aug 25 '22 at 15:37
  • It's not clear to me why you're using `JsonSerializer` here. If you want the JSON structured format representation of an event, use `JsonEventFormatter` - that's what it's there for. It is *not* expected that `CloudEvent` is serialized directly with JSON serializers. (You might also want to ask the event formatter to serialize the whole batch, if you're going to use the batch representation...) – Jon Skeet Aug 25 '22 at 16:23
  • 1
    This question is quite weird. First of all, I believe the error is because you're trying to serialize a generic collection of a generic type (`T` is a type, hence the error message). Second, it seems that what you ACTUALLY need is to know the "size" in bytes of your event list and what you're trying to achieve is to know if it is in "megabytes" or "kilobytes"... but why? If your code as you wrote it worked, it would return `true` if the list is 1 MB or 1023.99 MB long which is a HUGE difference. – Josh Part Aug 25 '22 at 16:45

0 Answers0