コード例 #1
0
        public void Visit(Thing thing, EventInfo @event, ThingEventAttribute?eventInfo)
        {
            if (!_isObjectStart)
            {
                _jsonWriter.StartObject("Events");
                _isObjectStart = true;
            }

            var name = eventInfo?.Name ?? @event.Name;

            _jsonWriter.StartObject(name);

            if (eventInfo != null)
            {
                _jsonWriter.PropertyWithNullableValue(nameof(ThingEventAttribute.Title), eventInfo.Title);
                _jsonWriter.PropertyWithNullableValue(nameof(ThingEventAttribute.Description), eventInfo.Description);
                _jsonWriter.PropertyWithNullableValue(nameof(ThingEventAttribute.Unit), eventInfo.Unit);
                _jsonWriter.PropertyType("@type", eventInfo.Type);
            }

            _jsonWriter.PropertyWithNullableValue("type", GetJsonType(GetEventType(@event.EventHandlerType)));

            _jsonWriter.StartArray("Links");
            _jsonWriter.StartObject();

            _jsonWriter.PropertyWithValue("href", $"/things/{_options.GetPropertyName(thing.Name)}/events/{_options.GetPropertyName(name)}");

            _jsonWriter.EndObject();
            _jsonWriter.EndArray();

            _jsonWriter.EndObject();
コード例 #2
0
        public void Intercept(Thing thing, PropertyInfo propertyInfo, ThingPropertyAttribute?thingPropertyAttribute)
        {
            if (!_isObjectStart)
            {
                _jsonWriter.StartObject("Properties");
                _isObjectStart = true;
            }

            var propertyName = thingPropertyAttribute?.Name ?? propertyInfo.Name;
            var propertyType = propertyInfo.PropertyType;
            var jsonType     = GetJsonType(propertyType);

            if (jsonType == null)
            {
                return;
            }

            _jsonWriter.StartObject(propertyName);

            if (thingPropertyAttribute != null)
            {
                _jsonWriter.PropertyWithNullableValue(nameof(ThingPropertyAttribute.Title),
                                                      thingPropertyAttribute.Title);
                _jsonWriter.PropertyWithNullableValue(nameof(ThingPropertyAttribute.Description),
                                                      thingPropertyAttribute.Description);
                _jsonWriter.PropertyWithNullableValue("ReadOnly", thingPropertyAttribute.IsReadOnly);
                _jsonWriter.PropertyWithNullableValue("Type", jsonType);
                _jsonWriter.PropertyEnum("@enum", propertyType, thingPropertyAttribute.Enum);
                _jsonWriter.PropertyWithNullableValue(nameof(ThingPropertyAttribute.Unit), thingPropertyAttribute.Unit);
                _jsonWriter.PropertyType("@type", thingPropertyAttribute.Type);

                if (jsonType == "number" || jsonType == "integer")
                {
                    _jsonWriter.PropertyNumber(nameof(ThingPropertyAttribute.Minimum), propertyType,
                                               thingPropertyAttribute.MinimumValue);
                    _jsonWriter.PropertyNumber(nameof(ThingPropertyAttribute.Maximum), propertyType,
                                               thingPropertyAttribute.MaximumValue);
                    _jsonWriter.PropertyNumber(nameof(ThingPropertyAttribute.MultipleOf), propertyType,
                                               thingPropertyAttribute.MultipleOfValue);
                }
            }



            _jsonWriter.StartArray("Links");

            _jsonWriter.StartObject();

            _jsonWriter.PropertyWithValue("href",
                                          $"/things/{_options.GetPropertyName(thing.Name)}/properties/{_options.GetPropertyName(propertyName)}");

            _jsonWriter.EndObject();
            _jsonWriter.EndArray();

            _jsonWriter.EndObject();
        }
コード例 #3
0
        public void Intercept(Thing thing, MethodInfo action, ThingActionAttribute?actionInfo)
        {
            if (!_isObjectStart)
            {
                _jsonWriter.StartObject("Actions");
                _isObjectStart = true;
            }

            var name = actionInfo?.Name ?? action.Name;

            _jsonWriter.StartObject(name);

            if (actionInfo != null)
            {
                _jsonWriter.PropertyWithNullableValue("Title", actionInfo.Title);
                _jsonWriter.PropertyWithNullableValue("Description", actionInfo.Description);
                _jsonWriter.PropertyType("@type", actionInfo.Type);
            }

            var parameters = action.GetParameters();

            if (parameters.Length > 0)
            {
                _jsonWriter.StartObject("Input");

                _jsonWriter.PropertyWithValue("Type", "object");

                _jsonWriter.StartObject("Properties");
                foreach (var parameter in parameters)
                {
                    if (parameter.GetCustomAttribute <FromServicesAttribute>() != null ||
                        parameter.ParameterType == typeof(CancellationToken))
                    {
                        continue;
                    }

                    _jsonWriter.StartObject(parameter.Name !);
                    var jsonType = GetJsonType(parameter.ParameterType);

                    if (jsonType == null)
                    {
                        throw new ArgumentException();
                    }

                    _jsonWriter.PropertyWithValue("Type", jsonType);
                    var parameterActionInfo = parameter.GetCustomAttribute <ThingParameterAttribute>();

                    if (parameterActionInfo != null)
                    {
                        _jsonWriter.PropertyWithNullableValue("Title", parameterActionInfo.Title);
                        _jsonWriter.PropertyWithNullableValue("Description", parameterActionInfo.Description);
                        _jsonWriter.PropertyWithNullableValue("Unit", parameterActionInfo.Unit);
                        if (jsonType == "number" || jsonType == "integer")
                        {
                            _jsonWriter.PropertyNumber(nameof(ThingPropertyAttribute.Minimum), parameter.ParameterType,
                                                       parameterActionInfo.MinimumValue);
                            _jsonWriter.PropertyNumber(nameof(ThingPropertyAttribute.Maximum), parameter.ParameterType,
                                                       parameterActionInfo.MaximumValue);
                            _jsonWriter.PropertyWithNullableValue(nameof(ThingPropertyAttribute.MultipleOf),
                                                                  parameterActionInfo.MultipleOfValue);
                        }
                    }

                    _jsonWriter.EndObject();
                }

                _jsonWriter.EndObject();
                _jsonWriter.EndObject();
            }
            else if (actionInfo?.Type != null)
            {
                _jsonWriter.StartObject("Input");
                _jsonWriter.PropertyType("@type", actionInfo.Type);
                _jsonWriter.EndObject();
            }

            _jsonWriter.StartArray("Links");
            _jsonWriter.StartObject();
            _jsonWriter.PropertyWithValue("href", $"/things/{_options.GetPropertyName(thing.Name)}/actions/{_options.GetPropertyName(name)}");
            _jsonWriter.EndObject();
            _jsonWriter.EndArray();
            _jsonWriter.EndObject();
        }