JamesNK/Newtonsoft.Json · error · ArgumentException

Could not determine JSON object type for type {0}.

Error message

Could not determine JSON object type for type {0}.

What it means

Thrown by the internal JValue.GetValueType helper when a .NET object is assigned into a JValue but its runtime type is not one of the supported primitive/category types (string, integer kinds, Enum, BigInteger, float/double/decimal, DateTime/DateTimeOffset, byte[], bool, Guid, Uri, TimeSpan). The {0} placeholder is value.GetType(). This indicates an attempt to wrap an unsupported CLR type as a scalar JValue, which only models JSON primitives.

Source

Thrown at Src/Newtonsoft.Json/Linq/JValue.cs:677

            }
            else if (value is bool)
            {
                return JTokenType.Boolean;
            }
            else if (value is Guid)
            {
                return JTokenType.Guid;
            }
            else if (value is Uri)
            {
                return JTokenType.Uri;
            }
            else if (value is TimeSpan)
            {
                return JTokenType.TimeSpan;
            }

            throw new ArgumentException("Could not determine JSON object type for type {0}.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }

        private static JTokenType GetStringValueType(JTokenType? current)
        {
            if (current == null)
            {
                return JTokenType.String;
            }

            switch (current.GetValueOrDefault())
            {
                case JTokenType.Comment:
                case JTokenType.String:
                case JTokenType.Raw:
                    return current.GetValueOrDefault();
                default:
                    return JTokenType.String;
            }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Serialize the object to a JToken first: JToken.FromObject(myPoco) yields a JObject/JArray, not a JValue.
  2. If you meant to store a string form, call myObject.ToString() (or JsonConvert) and wrap that string in a JValue.
  3. Add a custom JsonConverter that converts the unsupported type to a supported primitive before constructing the JValue.

Example fix

// before
var v = new JValue(new MyPoco());

// after
var token = JToken.FromObject(new MyPoco());
Defensive patterns

Strategy: validation

Validate before calling

var t = value.GetType();
if (!IsSupportedJValueType(t)) { token = JToken.FromObject(value); } else { token = new JValue(value); }

Type guard

static bool IsSupportedJValueType(Type t) => t.IsPrimitive || t.IsEnum || t == typeof(string) || t == typeof(decimal) || t == typeof(Guid) || t == typeof(Uri) || t == typeof(TimeSpan) || t == typeof(DateTime) || t == typeof(DateTimeOffset) || t == typeof(byte[]) || t == typeof(System.Numerics.BigInteger);

Try / catch

try { return new JValue(value); } catch (ArgumentException) { return JToken.FromObject(value); }

Prevention

When it happens

Trigger: Constructing new JValue(customObject) where customObject is an arbitrary class/struct/record that is not a primitive or one of the known value types, e.g. new JValue(new MyPoco()). Also triggered by reflection-driven code that forwards an unrecognised runtime type into a JValue constructor or WriteTo path that calls GetValueType.

Common situations: Trying to store a complex object directly as a JValue instead of serializing it to a JObject; bugs in custom JsonConverter implementations that build JValues from arbitrary input; assigning anonymous types.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/99240b5b0e8d89ea. Report an issue: GitHub.