// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) TypeSpec Code Generator.

package tsptest.armresourceprovider.models;

import com.azure.core.annotation.Fluent;
import com.azure.json.JsonReader;
import com.azure.json.JsonSerializable;
import com.azure.json.JsonToken;
import com.azure.json.JsonWriter;
import java.io.IOException;

/**
 * The CustomTemplateResourcePatch model.
 */
@Fluent
public final class CustomTemplateResourcePatch implements JsonSerializable<CustomTemplateResourcePatch> {
    /*
     * Managed identity.
     */
    private ManagedServiceIdentity identity;

    /**
     * Creates an instance of CustomTemplateResourcePatch class.
     */
    public CustomTemplateResourcePatch() {
    }

    /**
     * Get the identity property: Managed identity.
     * 
     * @return the identity value.
     */
    public ManagedServiceIdentity identity() {
        return this.identity;
    }

    /**
     * Set the identity property: Managed identity.
     * 
     * @param identity the identity value to set.
     * @return the CustomTemplateResourcePatch object itself.
     */
    public CustomTemplateResourcePatch withIdentity(ManagedServiceIdentity identity) {
        this.identity = identity;
        return this;
    }

    /**
     * Validates the instance.
     * 
     * @throws IllegalArgumentException thrown if the instance is not valid.
     */
    public void validate() {
        if (identity() != null) {
            identity().validate();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeJsonField("identity", this.identity);
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of CustomTemplateResourcePatch from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of CustomTemplateResourcePatch if the JsonReader was pointing to an instance of it, or null
     * if it was pointing to JSON null.
     * @throws IOException If an error occurs while reading the CustomTemplateResourcePatch.
     */
    public static CustomTemplateResourcePatch fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            CustomTemplateResourcePatch deserializedCustomTemplateResourcePatch = new CustomTemplateResourcePatch();
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("identity".equals(fieldName)) {
                    deserializedCustomTemplateResourcePatch.identity = ManagedServiceIdentity.fromJson(reader);
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedCustomTemplateResourcePatch;
        });
    }
}
