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

package type.model.inheritance.recursive.models;

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

/**
 * element.
 */
@Fluent
public class Element implements JsonSerializable<Element> {
    /*
     * The extension property.
     */
    @Generated
    private List<Extension> extension;

    /**
     * Creates an instance of Element class.
     */
    @Generated
    public Element() {
    }

    /**
     * Get the extension property: The extension property.
     * 
     * @return the extension value.
     */
    @Generated
    public List<Extension> getExtension() {
        return this.extension;
    }

    /**
     * Set the extension property: The extension property.
     * 
     * @param extension the extension value to set.
     * @return the Element object itself.
     */
    @Generated
    public Element setExtension(List<Extension> extension) {
        this.extension = extension;
        return this;
    }

    /**
     * {@inheritDoc}
     */
    @Generated
    @Override
    public JsonWriter toJson(JsonWriter jsonWriter) throws IOException {
        jsonWriter.writeStartObject();
        jsonWriter.writeArrayField("extension", this.extension, (writer, element) -> writer.writeJson(element));
        return jsonWriter.writeEndObject();
    }

    /**
     * Reads an instance of Element from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of Element 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 Element.
     */
    @Generated
    public static Element fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            Element deserializedElement = new Element();
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("extension".equals(fieldName)) {
                    List<Extension> extension = reader.readArray(reader1 -> Extension.fromJson(reader1));
                    deserializedElement.extension = extension;
                } else {
                    reader.skipChildren();
                }
            }

            return deserializedElement;
        });
    }
}
