JamesNK/Newtonsoft.Json · error · ArgumentException

Enum type {0} is not a set of flags.

Error message

Enum type {0} is not a set of flags.

What it means

GetFlagsValues<T> (EnumUtils.cs:94-125) decomposes a value into its constituent flag members. It requires the enum to carry [Flags]; it checks IsDefined(typeof(FlagsAttribute)) at EnumUtils.cs:98-101 and throws ArgumentException if the attribute is absent, because bitwise decomposition is meaningless for non-flags enums.

Source

Thrown at Src/Newtonsoft.Json/Utilities/EnumUtils.cs:100

#endif

                resolvedNames[i] = key.Value2 != null
                    ? key.Value2.GetPropertyName(resolvedName, hasSpecifiedName)
                    : resolvedName;
            }

            bool isFlags = enumType.IsDefined(typeof(FlagsAttribute), false);

            return new EnumInfo(isFlags, values, names, resolvedNames);
        }

        public static IList<T> GetFlagsValues<T>(T value) where T : struct
        {
            Type enumType = typeof(T);

            if (!enumType.IsDefined(typeof(FlagsAttribute), false))
            {
                throw new ArgumentException("Enum type {0} is not a set of flags.".FormatWith(CultureInfo.InvariantCulture, enumType));
            }

            Type underlyingType = Enum.GetUnderlyingType(value.GetType());

            ulong num = ToUInt64(value);
            EnumInfo enumNameValues = GetEnumValuesAndNames(enumType);
            IList<T> selectedFlagsValues = new List<T>();

            for (int i = 0; i < enumNameValues.Values.Length; i++)
            {
                ulong v = enumNameValues.Values[i];

                if ((num & v) == v && v != 0)
                {
                    selectedFlagsValues.Add((T)Convert.ChangeType(v, underlyingType, CultureInfo.CurrentCulture));
                }
            }

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Add [Flags] to the enum definition.
  2. If the type genuinely isn't a bit field, use a non-flags API (TryToString / direct cast) instead of GetFlagsValues.
  3. Verify the enum design before relying on bitwise composition.

Example fix

// before
enum Perm { Read = 1, Write = 2 } // no [Flags] -> GetFlagsValues throws
// after
[Flags]
enum Perm { Read = 1, Write = 2 }
Defensive patterns

Strategy: validation

Validate before calling

// Verify [Flags] before calling a flags-style API.
if (!enumType.IsDefined(typeof(FlagsAttribute), false))
    throw new ArgumentException($"{enumType} must have [Flags] to decompose.");

Type guard

static bool IsFlagsEnum(Type t) => t.IsEnum && t.IsDefined(typeof(FlagsAttribute), false);

Try / catch

try { EnumUtils.GetFlagsValues(value); } catch (ArgumentException ex) when (ex.Message.Contains("not a set of flags")) { /* add [Flags] or use a non-flags API */ }

Prevention

When it happens

Trigger: Calling GetFlagsValues on an enum that lacks the [Flags] attribute (e.g. a plain sequential enum used as if it were a bit field).

Common situations: Treating a regular enum as a bitmask; refactoring that removed [Flags] by accident; using flags-style APIs (StringEnumConverter with AllowIntegerValues + flags) on non-flags enums.

Related errors


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