Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misalignment in Json stringify result with pretty when the object has a parent class with no variables #11560

Closed
MadEwink opened this issue Feb 7, 2024 · 1 comment

Comments

@MadEwink
Copy link

MadEwink commented Feb 7, 2024

Haxe version : Tested on 4.3.2

Issue :
When using haxe.Json.stringify on a class which inherits a class with at least one method and no variables, pretty formatting will be broken with a missing line return and wrong indentation.
This is a minor issue that can safely be ignored.

Example :

class JsonPrettyClass {
    public static function main() {
        var p = new ParentClass();
        var c = new ChildClass();

        // If parent class had variables they would be last, thus causing no issue
        // trace("-- Parent class fields --");
        // for (f in Type.getInstanceFields(ParentClass)) {
        //     trace(f);
        // }

        // Child class field variables are before parent class methods
        // trace("-- Child class fields --");
        // for (f in Type.getInstanceFields(ChildClass)) {
        //     trace(f);
        // }

        var json = haxe.Json.stringify(c, "\t");
        trace(json);
    }
}

class ParentClass {
    // no field variable, the issue does not reproduce when there is one

    function anyFunc() {}

    public function new() {}
}

class ChildClass extends ParentClass {
    var anyVar : Int;
}

My understanding :
This is caused by the changes in this commit. In the specific case when the object which is passed to write happens to be an instance of a class which inherits a class with only methods and no variables, the if (i == last) condition in fieldsString is skipped.

Fix proposal :
fix-proposal.patch

@Simn Simn closed this as completed in 4463066 Feb 7, 2024
@Simn
Copy link
Member

Simn commented Feb 7, 2024

Thanks! That's a nice edge case which apparently went unnoticed for 10 years. I've applied the fix as you've suggested, and added your example as a test. We will also add this to 4.3.

kLabz pushed a commit that referenced this issue Feb 18, 2024
kLabz pushed a commit that referenced this issue Mar 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants