© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•13mo ago•
31 replies
Victor H

Parsing data from several sources into a common format.

I have several data sources that I get data from that I must parse into a common format.

Say this is the common format (simplified example):
public record ProductDto(
    string ProductId,
    Price Price,
);

public abstract record Price(decimal Value);
public sealed record FixedPrice(decimal Value) : Price(Value);
public sealed record PricePerUnit(decimal Value, string Unit) : Price(Value);
public record ProductDto(
    string ProductId,
    Price Price,
);

public abstract record Price(decimal Value);
public sealed record FixedPrice(decimal Value) : Price(Value);
public sealed record PricePerUnit(decimal Value, string Unit) : Price(Value);

One approach would be to set up something like a
public record Source1ProductDto
public record Source1ProductDto
and use annotations like
JsonPropertyName
JsonPropertyName
for each data source and let
System.Text.JsonSerializer
System.Text.JsonSerializer
handle it. However, for more complex types like
Price
Price
I might need to combine data from several fields of the original data to parse it into my custom format. My current approach is to use some type of strategy pattern for each field by using this interface:
public interface IFieldParser<out T>
{
    T Parse(JsonElement json);
}
public interface IFieldParser<out T>
{
    T Parse(JsonElement json);
}

then I do this (simplified parsing for illustration purpose):
public class Source1IdParser : IFieldParser<string>
{
    public string Parse(JsonElement)
    {
        // Here it might be called "id"
        return json.GetProperty("id").GetString() ?? string.Empty;
    }
}

public class Source2IdParser : IFieldParser<string>
{
    public string Parse(JsonElement json)
    {
        // Here it might be called "productCode"
        return json.GetProperty("productCode").GetString() ?? string.Empty;
    }
}

public class Source1PriceParser : IFieldParser<Price>
{
    public Price Parse(JsonElement json)
    {
        var unit = json.GetProperty("unit").GetString() ?? "";
        var amount = json.GetProperty("price").GetString() ?? "";
        return unit == "" 
                    ? new FixedPrice(Convert.ToDecimal(amount))
                    : new PricePerUnit(Convert.ToDecimal(amount), unit);
    }
}
public class Source1IdParser : IFieldParser<string>
{
    public string Parse(JsonElement)
    {
        // Here it might be called "id"
        return json.GetProperty("id").GetString() ?? string.Empty;
    }
}

public class Source2IdParser : IFieldParser<string>
{
    public string Parse(JsonElement json)
    {
        // Here it might be called "productCode"
        return json.GetProperty("productCode").GetString() ?? string.Empty;
    }
}

public class Source1PriceParser : IFieldParser<Price>
{
    public Price Parse(JsonElement json)
    {
        var unit = json.GetProperty("unit").GetString() ?? "";
        var amount = json.GetProperty("price").GetString() ?? "";
        return unit == "" 
                    ? new FixedPrice(Convert.ToDecimal(amount))
                    : new PricePerUnit(Convert.ToDecimal(amount), unit);
    }
}

You get the point for PriceParser2.

Can you give me advice on how to improve? Possibly how to leverage .NET strengths better.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

✅ Common sectioned file format
C#CC# / help
3y ago
Object Data Sources
C#CC# / help
3y ago
❔ Data Parsing
C#CC# / help
4y ago
Windows Forms Data Sources
C#CC# / help
2y ago