Skip to content

Property.toJSONObject drops defaults when there are no local entries #1086

Closed

Description

@PHJ2000

Property.toJSONObject loses inherited defaults when the Properties instance has no local entries. Adding an unrelated local property makes the same defaults appear.

The isEmpty() check only examines local entries, whereas propertyNames() and getProperty() include the defaults chain.

import java.util.Properties;
import org.json.Property;
public class Reproduce {
    public static void main(String[] args) {
        Properties defaults = new Properties();
        defaults.setProperty("timeout", "30");
        Properties config = new Properties(defaults);
        System.out.println("getProperty=" + config.getProperty("timeout"));
        System.out.println("defaults only=" + Property.toJSONObject(config));
        config.setProperty("host", "localhost");
        System.out.println("with unrelated override=" + Property.toJSONObject(config));
    }
}

Output:

getProperty=30
defaults only={}
with unrelated override={"host":"localhost","timeout":"30"}

Reproduced with 20260814 and master at 18e8ef6 on Java 21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions