magreenberg magreenberg - 3 months ago 29
C# Question

Writing to Dynamic JSON object

I have a pretty large JSON object that I have converted into dynamic object in c# using the method expressed in this post. To be clear I am using the register serializer method and not the one from the MVC library.

I can read these values just fine; however, I would like to able to update them with new information. Every time I try, the program crashes without much information as to why. Is there any way to do this without making a new class with a data contract?

How can I write values to this dynamic object?

EDIT

I must be referencing the value of the object incorrectly because I am getting a "{"'System.Dynamic.DynamicObject' does not contain a definition for 'password'"}" error message.

Here is the JSON

{
"cfgFileString": "Canopy configuration file",
"deviceType": "3.6GHz MIMO OFDM - Subscriber Module",
"cfgUtcTimestamp": "2011-01-01T00:01:59Z",
"cfgFileVersion": "1.0",
"srcMacAddress": "0a-00-3e-41-c3-a1",
"swVersion": "CANOPY 14.2 (Build 30) SM-DES",
"userParameters": {
"smSyncConfig": {
"timingPulseGated": 1
},
"smSyslogConfig": {
"syslogServerApPreferred": 1,
"syslogSMXmitControl": 0
},
"smQosConfig": {
"lowPriorityUplinkCIR": 0,
"lowPriorityDownlinkCIR": 0,
"highPriorityChannel": 0,
"bCastMIR": 0,
"bCastMIRUnits": 0
},
"authenticationConfig": {
"whispWebUserAccessMode": 0,
"allowRejectThenLocal": 0,
"authenticationKeyEncrypted": "37107535bd95272afceb980bdb2379cd9340f71d22f07b0bb988bd8741cf3fdbf2b7da94b7481a4df0b5ed1fc2f8d7b4",
"authKeyOption": 0,
"accounts": [
{
"userName": "admin",
"level": 3,
"readOnly": false,
"password": ""
},
{
"userName": "root",
"level": 3,
"readOnly": false,
"password": ""
}
]
},
"smNetworkConfig": {
"networkAccess": 0,
"enable8023link": 1
},
"radioConfig": {
"installationColorCode": 1,
"antennaGain": 0,
"factoryResetOnDefaultPlug": 0,
"receiveQualityDebug": 0,
"atexHazlocPowerLimit": 4,
"regionCode": 0,
"customFrequencies": [
3652500,
3655000,
3657500,
3660000,
3662500,
3665000,
3667500,
3670000,
3672500,
3675000,
3677500,
3680000,
3682500,
3685000,
3687500,
3690000,
3692500,
3695000,
3697500
]
},
"smRadioConfig": {
"powerUpMode": 0,
"apSelection": 0,
"frequencyScanList": [
3652500,
3655000,
3657500,
3660000,
3662500,
3665000,
3667500,
3670000,
3672500,
3675000,
3677500,
3680000,
3682500,
3685000,
3687500,
3690000,
3692500,
3695000,
3697500
],
"bandwidthScanList": [
50,
70,
100,
150,
200,
300,
400
],
"colorCodeList": [
{
"colorCode": 0,
"priority": 1
}
]
},
"smAuthenticationConfig": {
"authenticationEnforce": 0,
"phase1": 0,
"phase2": 2,
"authOuterId": "anonymous",
"authPasswordEncrypted": "ba9122c25780be0f289120c382646277",
"authUsername": "0a-00-3e-41-c3-a1",
"useRealm": 0,
"realm": "canopy.net",
"certificates": [
"-----BEGIN CERTIFICATE-----\nMIIDSDCCArGgAwIBAgIBADANBgkqhkiG9w0BAQUFADCBwDELMAkGA1UEBhMCVVMx\nETAPBgNVBAgTCElsbGlub2lzMSEwHwYDVQQKExhNb3Rvcm9sYSBTb2x1dGlvbnMs\nIEluYy4xIjAgBgNVBAsTGUNhbm9weSBXaXJlbGVzcyBCcm9hZGJhbmQxIjAgBgNV\nBAMTGUNhbm9weSBBQUEgU2VydmVyIERlbW8gQ0ExMzAxBgkqhkiG9w0BCQEWJHRl\nY2huaWNhbC1zdXBwb3J0QGNhbm9weXdpcmVsZXNzLmNvbTAeFw0wMTAxMDEwMDAw\nMDBaFw00OTEyMzEyMzU5NTlaMIHAMQswCQYDVQQGEwJVUzERMA8GA1UECBMISWxs\naW5vaXMxITAfBgNVBAoTGE1vdG9yb2xhIFNvbHV0aW9ucywgSW5jLjEiMCAGA1UE\nCxMZQ2Fub3B5IFdpcmVsZXNzIEJyb2FkYmFuZDEiMCAGA1UEAxMZQ2Fub3B5IEFB\nQSBTZXJ2ZXIgRGVtbyBDQTEzMDEGCSqGSIb3DQEJARYkdGVjaG5pY2FsLXN1cHBv\ncnRAY2Fub3B5d2lyZWxlc3MuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\ngQDAoL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b8K4H9MPSB/LHv9i61JUMYECgiWXxz8sNTEbZnFcWB5hgb5ZPr2g\nbaqr8X9KRoiZ0MSzEzQsEkB79EGTyNvVItH2GQIDAQABo3cwdTAJBgNVHRMEAjAA\nMCgGCWCGSAGG+EIBDQQbFhlJbnNlY3VyZSBEZW1vIENlcnRpZmljYXRlMB0GA1Ud\nDgQWBBTaTn3PsiRFNAhrpYkzvZ7cfPhIdjAfBgNVHSMEGDAWgBTaTn3PsiRFNAhr\npYkzvZ7cfPhIdjANBgkqhkiG9w0BAQUFAAOBgQC7qaY808dmS7WUfKnAoblH2wZE\nHKjg6ivHl0GWeguhyuumhQDlkYoEsDesZa5FhuI6AUHpVnga1eNEGZ6es0/YHrVR\n5ZZkx7svAOEGo5mkc+j/cj1+zqRHCbdznGZU0gnL3Y0MV4oKcyhzGHL3dfwjlwA3\nZzSw0lSJRnC+dK4iBw==\n-----END CERTIFICATE-----\n"
]
},
"networkConfig": {
"webAutoUpdate": 0,
"bridgeEntryTimeout": 25,
"lanDhcpState": 0,
"dnsIpState": 1,
"sessionTimeout": 600,
"lldpBroadcastEnable": 0,
"ethernetLinkSpeed": 15,
"dnsPrimaryMgmtIP": "0.0.0.0",
"dnsAlternateMgmtIP": "0.0.0.0",
"dnsMgmtDomainName": "example.com",
"ftpPort": 21,
"httpPort": 80,
"httpsPort": 443,
"webAccess": 0,
"snmpStatus": 1,
"telnetStatus": 1,
"ftpStatus": 1,
"tftpStatus": 1,
"enableSecurityBanner": 0,
"textSecurityBanner": "example.com",
"acceptSecurityBanner": 1,
"packetFilterPppoe": 0,
"packetFilterSmb": 0,
"packetFilterSmbIpv6": 0,
"packetFilterSnmp": 0,
"packetFilterSnmpIpv6": 0,
"packetFilterUser1": 0,
"packetFilterUser2": 0,
"packetFilterUser3": 0,
"packetFilterOtherIpv4": 0,
"packetFilterAllIpv6Others": 0,
"packetFilterAllIpv4": 0,
"packetFilterAllIpv6": 0,
"packetFilterArp": 0,
"packetFilterAllOthers": 0,
"packetFilterBootpClient": 0,
"packetFilterBootpClientIpv6": 0,
"packetFilterBootpServer": 0,
"packetFilterBootpServerIpv6": 0,
"packetFilterMulticastIpv4": 0,
"packetFilterMulticastIpv6": 0,
"packetFilterDirection": 0,
"lanIp": "169.254.1.1",
"lanMask": "255.255.0.0",
"lanGateway": "169.254.0.0",
"diffServ": {
"1": 0,
"2": 0,
"3": 0,
"4": 4,
"5": 4,
"6": 4,
"7": 4,
"8": 0,
"9": 0,
"10": 0,
"11": 0,
"12": 4,
"13": 4,
"14": 4,
"15": 4,
"16": 0,
"17": 0,
"18": 0,
"19": 0,
"20": 4,
"21": 4,
"22": 4,
"23": 4,
"24": 0,
"25": 0,
"26": 0,
"27": 0,
"28": 4,
"29": 4,
"30": 4,
"31": 4,
"32": 0,
"33": 0,
"34": 0,
"35": 0,
"36": 4,
"37": 4,
"38": 4,
"39": 4,
"40": 0,
"41": 0,
"42": 0,
"43": 0,
"44": 4,
"45": 4,
"46": 4,
"47": 4,
"49": 0,
"50": 0,
"51": 0,
"52": 4,
"53": 4,
"54": 4,
"55": 4,
"57": 0,
"58": 0,
"59": 0,
"60": 4,
"61": 4,
"62": 4,
"63": 4
},
"portFiltering": []
},
"location": {
"siteInfoViewable": 1,
"siteName": "No Site Name",
"siteContact": "No Site Contact",
"siteLocation": "No Site Location",
"latitude": "+0.000000",
"longitude": "+0.000000",
"height": 0
},
"pppoeConfig": {
"pppoeCtlPriority": 0
},
"snmpConfig": {
"snmpMibPerm": 1,
"commStringRW": "Canopy",
"commStringROnly": "Canopyro",
"snmpv3EngineId": "800000a1030a003e41c3a1",
"snmpv3SecLvl": 0,
"snmpv3AuthProt": 0,
"snmpv3PrivProt": 0,
"roUserName": "Canopyro",
"roAuthPasswordEncrypted": "c56cd33e08dfd4e777b8c1759300090a",
"roPrivPasswordEncrypted": "4707d0d082fc4f9055d2d456ac74993cd445815b998dadd32d87cecf6eda8db7",
"rwUserEnable": 0,
"rwUserName": "Canopy",
"rwAuthPasswordEncrypted": "0931a4cd1c23f47e08b43aebfdf3784c",
"rwPrivPasswordEncrypted": "2c6e48b411b2cb1b99103b22986cf88b",
"snmpv3TrapEnable": 0,
"snmpv2cEnable": 1,
"trapDomainNameAppend": 0,
"snmpPort": 161,
"snmpTrapPort": 162,
"snmpIpAccessFilter": [
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
},
{
"address": "0.0.0.0",
"netmask": 0
}
],
"snmpTrapAddresses": [
"0.0.0.0",
"0.0.0.0",
"0.0.0.0",
"0.0.0.0",
"0.0.0.0",
"0.0.0.0",
"0.0.0.0",
"0.0.0.0",
"0.0.0.0",
"0.0.0.0"
]
},
"rfSecurityConfig": {
"encryptionConfig": 0
},
"smSpectrumAnalysisConfig": {
"spectrumAnalysisOnBoot": 0
},
"smNatNetworkConfig": {
"naptPublicIP": "0.0.0.0",
"naptPublicSubnetMask": "255.255.255.0",
"naptPublicGatewayIP": "0.0.0.0",
"naptRFPublicIP": "0.0.0.0",
"naptRFPublicSubnetMask": "255.255.255.0",
"naptRFPublicGateway": "0.0.0.0",
"naptEnable": 0,
"arpCacheTimeout": 20,
"tcpGarbageCollectTmout": 120,
"udpGarbageCollectTmout": 4,
"natTslTableSize": 2048,
"dhcpServerEnable": 1,
"dhcpServerLeaseTime": 30,
"dhcpIPStart": "169.254.1.2",
"dnsAutomatic": 1,
"preferredDNSIP": "0.0.0.0",
"alternateDNSIP": "0.0.0.0",
"natDNSProxyEnable": 0,
"dmzIP": "169.254.1.52",
"dmzEnable": 0,
"dhcpNumIPsToLease": 50,
"rfDhcpState": 0,
"natConnectionType": 1,
"wanPingReplyEnable": 0,
"naptRemoteManage": 0,
"portMapping": []
},
"qosConfig": {
"prioritizeTcpAck": 1,
"largeVCQ": 0,
"sustainedUplinkDataRate": 10000,
"sustainedDownlinkDataRate": 10000,
"uplinkBurstAllocation": 2500000,
"downlinkBurstAllocation": 2500000,
"maxUplinkDataRate": 0,
"maxDownlinkDataRate": 0
},
"gpsConfig": {},
"syslogConfig": {
"syslogMinLevelApPreferred": 1,
"syslogMinLevel": 6
},
"networkSecurityConfig": {
"ipAccessFilterEnabled": 0,
"ipAccessFilter": [
{
"address": "0.0.0.0",
"netmask": 32
},
{
"address": "0.0.0.0",
"netmask": 32
},
{
"address": "0.0.0.0",
"netmask": 32
}
]
},
"smVlanConfig": {
"ingressVID": 1,
"ingressVIDPriority": 0,
"ingressVIDPriorityMode": 0,
"providerVIDPriority": 0,
"providerVIDPriorityMode": 0,
"allowVIDAccess": 0,
"portVids": [
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
},
{
"macAddress": "00-00-00-00-00-00",
"vid": 1
}
]
},
"smPppoeConfig": {
"pppoeEnable": 0,
"pppoeAuthenticationType": 0,
"pppoeAccessConcentrator": "",
"pppoeServiceName": "",
"pppoeUserName": "",
"pppoePassword": "",
"pppoeTCPMSSClampEnable": 0,
"pppoeMTUOverrideEnable": 0,
"pppoeMTUOverrideValue": 1492,
"pppoeTimerType": 0,
"pppoeTimeoutPeriod": 30
},
"smNetworkSecurityConfig": {
"ethAccessEnable": 1
},
"vlanConfig": {
"vlanAcceptQinQFrames": 0,
"providerVID": 1,
"vlanPortType": 0,
"dynamicLearning": 1,
"managementVID": 1,
"managementVIDPriority": 0,
"managementVIDPriorityMode": 0,
"agingTimeout": 25,
"frameType": 0,
"priorityPrecedence": 0,
"vlanMembership": [],
"vlanRemark": {}
}
}
}


Here is what I am running

// register converter
serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
dynamic obj = serializer.Deserialize(result, typeof(object));


obj.userParameters.authenticationConfig.accounts[0].password = "testing";

Answer

I was able to reproduce your problem, by including the Serializer that you are using from the previous post. I figured out what's the problem:

That Serializer is read-only, because the class DynamicJsonObject doesn't implement the TrySetMember method, and hence it defaults to the base method in DynamicObject. Therefore, it doesn't support "Setting" of values. But this is very easy to remediate. All you have to do is to add the following method to the DynamicJsonObject class in the DynamicJsonConverter.cs file:

public override bool TrySetMember(SetMemberBinder binder, object value)
{
    _dictionary[binder.Name] = value;
    return true;
}

After making that change, I verified that this solves the problem, and you will be able to set any value as per your example.