Showing posts with label c# - JSON. Show all posts
Showing posts with label c# - JSON. Show all posts

Tuesday, November 4, 2025

c# - How to force JSON enum to save as TEXT instead of a NUMBER

This enum property:
public enum MinimizeActionEnum {
  NONE = 0,
  CLEAR = 1
}
    
public MinimizeActionEnum MinimizeAction { get; set; } = MinimizeActionEnum.NONE;
It is saved as INT by default:
"ReportParams": [
{
  "Name": "pProdId",
  "Description": "",
  "MinimizeAction": 0
},
With using that attribute:
[JsonConverter(typeof(JsonStringEnumConverter))]
public MinimizeActionEnum MinimizeAction { get; set; } = MinimizeActionEnum.NONE;
the enum value will be stored as TEXT:
"ReportParams": [
{
  "Name": "pProdId",
  "Description": "",
  "MinimizeAction": NONE
},

Monday, October 13, 2025

c# - How to solve JSON deserialization problem

JSON deserialization returns this error:
Each parameter in the deserialization constructor on type 
'cieb_AXReportPrintDLG.ReportParamComboValues' must bind 
to an object property or field on deserialization. Each 
parameter name must match with a property or field on the 
object. Fields are only considered when 
'JsonSerializerOptions.IncludeFields' is enabled. 
The match can be case-insensitive.
All fields looks like OK. The problem is in the parameter names in the constructor. This version returns error:
public class ReportParamComboValues {

  /* description - for text */
  public string Description { get; set; }

  /* returned value for SSRS report param */
  public string Value { get; set; }

  public ReportParamComboValues( string _description, string _value ) {  
    this.Description = _description;
    this.Value = _value;
  }

}
This is working:
public class ReportParamComboValues {

  /* description - for text */
  public string Description { get; set; }

  /* returned value for SSRS report param */
  public string Value { get; set; }

  public ReportParamComboValues( string Description, string Value ) {  
    this.Description = Description;
    this.Value = Value;
  }

}