0

I am gettin the json file stored as ConfigMap in K8s cluster. So I do not have the path as required in ConfigurationBuilder class in C#. Is there a way to build configuration from JSON object itself rather than the filepath ?

1

1 Answer 1

3

If you are able to get the JSON string from the source, you can call ConfigurationBuild.Add method by passing it an object of JsonStreamConfigurationSource class.

Following is the sample code.

ConfigurationBuilder builder = new ConfigurationBuilder();

// GetConfigJson method should get the JSON string from the source.
// I am leaving the implementation of that method up to you.
string jsonData = GetConfigJson();
    
// Load the JSON into MemoryStream
var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));
    
// Create an object of JsonStreamConfigurationSource class.
var configSource = new JsonStreamConfigurationSource();

// Assign stream to it.
configSource.Stream = stream;

// Call Add method of builder by passing the configSource object.
builder.Add(configSource);
    

You can also call AddJsonStream method on builder by passing the stream to it..

ConfigurationBuilder builder = new ConfigurationBuilder();

// GetConfigJson method should get the JSON string from the source.
// I am leaving the implementation of that method up to you.
string jsonData = GetConfigJson();
    
// Load the JSON into MemoryStream
var stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonData));

// Call AddJsonStream method of builder by passing the stream object.
builder.AddJsonStream(stream);
    

I hope this will help resolve you issue.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.