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

package azure.versioning.previewversion.models;

import com.azure.core.annotation.Generated;
import com.azure.core.annotation.Immutable;
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;

/**
 * The ListWidgetsResponse model.
 */
@Immutable
public final class ListWidgetsResponse implements JsonSerializable<ListWidgetsResponse> {
    /*
     * The widgets property.
     */
    @Generated
    private final List<Widget> widgets;

    /**
     * Creates an instance of ListWidgetsResponse class.
     * 
     * @param widgets the widgets value to set.
     */
    @Generated
    private ListWidgetsResponse(List<Widget> widgets) {
        this.widgets = widgets;
    }

    /**
     * Get the widgets property: The widgets property.
     * 
     * @return the widgets value.
     */
    @Generated
    public List<Widget> getWidgets() {
        return this.widgets;
    }

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

    /**
     * Reads an instance of ListWidgetsResponse from the JsonReader.
     * 
     * @param jsonReader The JsonReader being read.
     * @return An instance of ListWidgetsResponse if the JsonReader was pointing to an instance of it, or null if it was
     * pointing to JSON null.
     * @throws IllegalStateException If the deserialized JSON object was missing any required properties.
     * @throws IOException If an error occurs while reading the ListWidgetsResponse.
     */
    @Generated
    public static ListWidgetsResponse fromJson(JsonReader jsonReader) throws IOException {
        return jsonReader.readObject(reader -> {
            List<Widget> widgets = null;
            while (reader.nextToken() != JsonToken.END_OBJECT) {
                String fieldName = reader.getFieldName();
                reader.nextToken();

                if ("widgets".equals(fieldName)) {
                    widgets = reader.readArray(reader1 -> Widget.fromJson(reader1));
                } else {
                    reader.skipChildren();
                }
            }
            return new ListWidgetsResponse(widgets);
        });
    }
}
