public override object ReadJson()

in shared/src/TwinJsonConverter.cs [197:333]


        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            if (serializer == null)
            {
                throw new ArgumentNullException(nameof(serializer));
            }

            var twin = new Twin();

            if (reader.TokenType != JsonToken.StartObject)
            {
                return null;
            }

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    break;
                }

                if (reader.TokenType != JsonToken.PropertyName)
                {
                    // TODO: validate that this code is not reached.
                    continue;
                }

                string propertyName = reader.Value as string;
                reader.Read();

                switch (propertyName)
                {
                    case DeviceIdJsonTag:
                        twin.DeviceId = reader.Value as string;
                        break;

                    case ModelId:
                        twin.ModelId = reader.Value as string;
                        break;

                    case ModuleIdJsonTag:
                        twin.ModuleId = reader.Value as string;
                        break;

                    case ConfigurationsJsonTag:
                        twin.Configurations = serializer.Deserialize<Dictionary<string, ConfigurationInfo>>(reader);
                        break;

                    case CapabilitiesJsonTag:
                        Dictionary<string, object> capabilitiesDictionary = serializer.Deserialize<Dictionary<string, object>>(reader);
                        twin.Capabilities = new DeviceCapabilities
                        {
                            IotEdge = capabilitiesDictionary.ContainsKey(IotEdgeName) && (bool)capabilitiesDictionary[IotEdgeName]
                        };
                        break;

                    case ETagJsonTag:
                        twin.ETag = reader.Value as string;
                        break;

                    case TagsJsonTag:
                        if (reader.TokenType != JsonToken.StartObject)
                        {
                            throw new InvalidOperationException("Tags Json not a Dictionary.");
                        }
                        twin.Tags = new TwinCollection(JToken.ReadFrom(reader) as JObject);
                        break;

                    case PropertiesJsonTag:
                        PopulatePropertiesForTwin(twin, reader);
                        break;

                    case VersionTag:
                        twin.Version = (long?)reader.Value;
                        break;

                    case StatusTag:
                        string status = reader.Value as string;
                        twin.Status = status?[0] == '\"' ? JsonConvert.DeserializeObject<DeviceStatus>(status, JsonSerializerSettingsInitializer.GetJsonSerializerSettings()) : serializer.Deserialize<DeviceStatus>(reader);
                        break;

                    case StatusReasonTag:
                        twin.StatusReason = reader.Value as string;
                        break;

                    case StatusUpdateTimeTag:
                        twin.StatusUpdatedTime = ConvertToDateTime(reader.Value);
                        break;

                    case ConnectionStateTag:
                        string connectionState = reader.Value as string;
                        twin.ConnectionState = connectionState?[0] == '\"'
                            ? JsonConvert.DeserializeObject<DeviceConnectionState>(connectionState, JsonSerializerSettingsInitializer.GetJsonSerializerSettings())
                            : serializer.Deserialize<DeviceConnectionState>(reader);
                        break;

                    case LastActivityTimeTag:
                        twin.LastActivityTime = ConvertToDateTime(reader.Value);
                        break;

                    case CloudToDeviceMessageCountTag:
                        twin.CloudToDeviceMessageCount = serializer.Deserialize<int>(reader);
                        break;

                    case AuthenticationTypeTag:
                        string authenticationType = reader.Value as string;
                        twin.AuthenticationType = authenticationType?[0] == '\"'
                            ? JsonConvert.DeserializeObject<AuthenticationType>(authenticationType, JsonSerializerSettingsInitializer.GetJsonSerializerSettings())
                            : serializer.Deserialize<AuthenticationType>(reader);
                        break;

                    case X509ThumbprintTag:
                        twin.X509Thumbprint = serializer.Deserialize<X509Thumbprint>(reader);
                        break;

                    case DeviceScope:
                        twin.DeviceScope = serializer.Deserialize<string>(reader);
                        break;

                    case ParentScopes:
                        twin.ParentScopes = serializer.Deserialize<IReadOnlyList<string>>(reader);
                        break;

                    default:
                        // Ignore unknown fields
                        reader.Skip();
                        break;
                }
            }

            return twin;
        }