I am trying to serialize any type in JS to object in C#, and nothing seems to work.
I have a property on my EventAction
public DotNetObjectReference<EventAction<T>> DotNetObjRef { get; }
which is used to call an event from JS code. An example would be:
function onCellValueChanged(data, handler) {
var mapped = {
columnName: data.column.colId // string
rowIndex: data.rowIndex, // number
oldValue: data.oldValue, // any
newValue: data.newValue, // any
};
handler.dotNetObjRef.invokeMethodAsync('Invoke', mapped);
}
The calling method part works fine. The serialization is only partially done. In above example, I have a class called
public class CellValueChangedArgs
{
public string ColumnName { get; set; } = string.Empty;
public int RowIndex { get; set; }
public object? OldValue { get; set; }
public object? NewValue { get; set; }
}
and when instance of this class is called, string and int properties are serialized properly, object? types are not (they contain JsonElement instance instead of actual value, which can be string, number, datetime...).
If I change NewValue and OldValue types to string, then it works.
Anyone has any idea why? Is it a proper way to serialize it to string, and then dial with the conversion manually?