Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

List of objects in json configuration issue #727

@aodpi

Description

@aodpi

I'm creating a json file that I wish to add in configuration through configuration.AddJsonFile() with the following structure:

{
  "JsonLocalizerOptions": {
    "SupportedLanguages": [
      {
        "Identifier": "ro-MD",
        "Name": "Romana"
      }
    ]
  }
}

then I have a POCO class for it:

public class JsonLocalizerOptions
{
    #region Private backing fields (Default Values)
    private List<Language> supportedLanguages = new List<Language>()
    {
        new Language
        {
            Identifier = "en",
            Name = "English"
        },

        new Language
        {
            Identifier = "ro",
            Name = "Română"
        },

        new Language
        {
            Identifier ="ru",
            Name ="Русский"
        }
    };
    #endregion

    public JsonLocalizerOptions() { }

    #region Public Properties
    public List<Language> SupportedLanguages
    {
        get => supportedLanguages;
        set => supportedLanguages = value;
    }
    #endregion
}

The problem is that when I call services.Configure<JsonLocalizerOptions>(Configuration.GetSection(nameof(JsonLocalizerOptions))) the value object from the setter of the property SupportedLanguages contains the default list of the languages plus that one object from the json file. I think this is bizarre, I thought that when I call Configure I should receive the data from the json file, not my current data appending that one from json or the same data from the private field even though the setter is accessed.

capture

Is this really how it should work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions