Anuken/Mindustry · error · SerializationException

Expecting an object, but found: '${jsonMap}'

Error message

Expecting an object, but found: '${jsonMap}'

What it means

ContentParser.readFields expects a JSON object (a map of name/value pairs) so it can map each child onto a reflective field of the target object. If the supplied JsonValue is an array, string, number, or bool, it cannot be iterated as fields and a SerializationException is thrown.

Source

Thrown at core/src/mindustry/mod/ContentParser.java:1269

                if(!field.field.isAnnotationPresent(Nullable.class) && field.field.get(object) == null && !implicitNullable.contains(field.field.getType())){
                    throw new RuntimeException("'" + field.field.getName() + "' in " +
                        ((object.getClass().isAnonymousClass() ? object.getClass().getSuperclass() : object.getClass()).getSimpleName()) +
                        " is missing! " + object + "." + field.field.getName() + " cannot be null.");
                }
            }catch(Exception e){
                throw new RuntimeException(e);
            }
        });
    }

    private void readFields(Object object, JsonValue jsonMap, boolean stripType){
        if(stripType) jsonMap.remove("type");
        readFields(object, jsonMap);
    }

    void readFields(Object object, JsonValue jsonMap){
        if(!jsonMap.isObject()) throw new SerializationException("Expecting an object, but found: '" + jsonMap + "'");
        JsonValue research = jsonMap.remove("research");

        toBeParsed.remove(object);
        var type = object.getClass();
        var fields = parser.getFields(type);
        for(JsonValue child = jsonMap.child; child != null; child = child.next){
            FieldMetadata metadata = fields.get(child.name().replace(" ", "_"));
            if(metadata == null){
                if(ignoreUnknownFields){
                    warn("@Unknown field '@' for class '@'", currentContent == null ? "" : "[" + currentContent.minfo.sourceFile.name() + "]: ", child.name, type.getSimpleName());
                    continue;
                }else{
                    SerializationException ex = new SerializationException("Field not found: " + child.name + " (" + type.getName() + ")");
                    ex.addTrace(child.trace());
                    throw ex;
                }
            }
            Field field = metadata.field;

View on GitHub (pinned to f695ad7e60)

Solutions

  1. Inspect the reported jsonMap value and confirm the surrounding field expects an object.
  2. Rewrite the JSON node as an object literal { ... }.
  3. Check the field type in the target class — if it expects a collection, the object-vs-array shape may be inverted.
  4. Enable ignoreUnknownFields/logging to see which parent field led here.

Example fix

// before (array where object expected)
"requirements": [ ["copper", 10] ]

// after (object form the parser expects)
"requirements": { "copper": 10 }
Defensive patterns

Strategy: validation

Validate before calling

// Confirm a JsonValue is an object before reading fields.
if(jsonMap == null || !jsonMap.isObject()) {
    throw new IllegalArgumentException("Expected JSON object, got: " + (jsonMap == null ? "null" : jsonMap.type()));
}

Type guard

boolean isJsonObject(JsonValue v){ return v != null && v.isObject(); }

Try / catch

try {
    parser.readFields(obj, jsonMap);
} catch(SerializationException e) {
    if(e.getMessage().startsWith("Expecting an object")) { /* fix JSON shape */ }
    else throw e;
}

Prevention

When it happens

Trigger: A mod JSON value that must be an object is instead an array or scalar — e.g. a block definition whose body is a list, or a nested content field resolved to a primitive. Reached whenever readFields(object, jsonMap) is called with a non-object JsonValue.

Common situations: Wrong JSON structure (array where object expected); copy-paste from a list-style example; inline content defined with [] instead of {}; a 'type' resolution that produced a non-object value.

Related errors


AI-assisted analysis of Anuken/Mindustry@f695ad7e60 (2026-08-14). Data as JSON: /api/errors/e5dd1a0c2ca141a3. Report an issue: GitHub.