Newtonsoft.json: InvalidProgramException "Cannot create boxed ByRef-like values" for netcoreapp2.1

Created on 2 Jul 2018  路  5Comments  路  Source: JamesNK/Newtonsoft.Json

I changed the TargetFramework of an unit test from netcoreapp2.0 to netcoreapp2.1 without any further changes and this error happened on serializing:

Result StackTrace:  
at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer, Object value, JsonContainerContract contract, JsonProperty member, JsonProperty property, JsonContract& memberContract, Object& memberValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeDictionary(JsonWriter writer, IDictionary values, JsonDictionaryContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
   at MyUnitTest
--- End of stack trace from previous location where exception was thrown ---
----- Inner Stack Trace -----
   at System.Runtime.CompilerServices.RuntimeHelpers._CompileMethod(IRuntimeMethodInfo method)
   at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
   at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
   at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
   at Newtonsoft.Json.Utilities.ExpressionReflectionDelegateFactory.CreateGet[T](PropertyInfo propertyInfo)
   at Newtonsoft.Json.Serialization.ExpressionValueProvider.GetValue(Object target)
Result Message: 
Newtonsoft.Json.JsonSerializationException : Error getting value from 'Preamble' on 'System.Text.ConsoleEncoding'.
---- System.InvalidProgramException : Cannot create boxed ByRef-like values.

Googling this error only came up with this one.

Most helpful comment

Fixed. ref struct properties values are skipped instead of throwing an error.

All 5 comments

Could you add a repro of the issue? I'm not even sure how to go about serializing ConsoleEncoding.

I'm not entirely sure where in my object graph a reference to ConsoleEncoding popped up, but the exception can be reproduced this way:

    object data = Console.InputEncoding;
    var json = JsonConvert.SerializeObject(data);
    Console.WriteLine(json);

The code above works on dotnetcoreapp1.0, dotnetcoreapp1.1, net4xx. (and fails on dotnetcoreapp2.0 with a different problem, so on that platform some other class which my code is serializing might not have a reference to ConsoleEncoding at all there yet).

But for sure the amount of these by-ref types (maybe a Span) will increase rapidly on netcore for constructor overloads etc.

I've the same issue (netcoreapp2.1) when serializing System.Text.UTF8Encoding.

The error message is

Error getting value from 'Preamble' on 'System.Text.UTF8Encoding'. ---> System.InvalidProgramException: Cannot create boxed ByRef-like values

I have the same - as above - at runtime. My unit tests seem to serialise UTF8Encoding just fine, but at runtime, it fails on the preamble property (a ReadOnlySpan)

Edit: this was because my test project was still netcoreapp2.0. On netcoreapp2.1 it repro's each time

Fixed. ref struct properties values are skipped instead of throwing an error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Phreak87 picture Phreak87  路  11Comments

Richard-Payne picture Richard-Payne  路  13Comments

danebou picture danebou  路  18Comments

svick picture svick  路  34Comments

azzlack picture azzlack  路  38Comments