示例#1
0
        /// <summary>
        /// Return a JSON representation of this object.
        /// </summary>
        /// <param name="CustomConnectorSerializer">A delegate to serialize custom connector JSON objects.</param>
        public JObject ToJSON(CustomJObjectSerializerDelegate <Connector> CustomConnectorSerializer = null)
        {
            var JSON = JSONObject.Create(

                new JProperty("id", Id.ToString()),
                new JProperty("standard", Standard.ToString()),
                new JProperty("format", Format.ToString()),
                new JProperty("power_type", PowerType.ToString()),
                new JProperty("max_voltage", MaxVoltage),
                new JProperty("max_amperage", MaxAmperage),

                MaxElectricPower.HasValue
                               ? new JProperty("max_electric_power", MaxElectricPower.Value)
                               : null,

                TariffIds.SafeAny()
                               ? new JProperty("tariff_ids", new JArray(TariffIds.Select(tarifId => tarifId.ToString())))
                               : null,

                TermsAndConditionsURL.HasValue
                               ? new JProperty("terms_and_conditions", TermsAndConditionsURL.ToString())
                               : null,

                new JProperty("last_updated", LastUpdated.ToIso8601())

                );

            return(CustomConnectorSerializer != null
                       ? CustomConnectorSerializer(this, JSON)
                       : JSON);
        }
示例#2
0
        /// <summary>
        /// Try to parse the given JSON representation of a connector.
        /// </summary>
        /// <param name="JSON">The JSON to parse.</param>
        /// <param name="Connector">The parsed connector.</param>
        /// <param name="ErrorResponse">An optional error response.</param>
        /// <param name="ConnectorIdURL">An optional connector identification, e.g. from the HTTP URL.</param>
        /// <param name="CustomConnectorParser">A delegate to parse custom connector JSON objects.</param>
        public static Boolean TryParse(JObject JSON,
                                       out Connector Connector,
                                       out String ErrorResponse,
                                       Connector_Id?ConnectorIdURL = null,
                                       CustomJObjectParserDelegate <Connector> CustomConnectorParser = null)
        {
            try
            {
                Connector = default;

                if (JSON?.HasValues != true)
                {
                    ErrorResponse = "The given JSON object must not be null or empty!";
                    return(false);
                }

                #region Parse Id                  [mandatory]

                if (JSON.ParseOptionalStruct("id",
                                             "connector identification",
                                             Connector_Id.TryParse,
                                             out Connector_Id? ConnectorIdBody,
                                             out ErrorResponse))
                {
                    if (ErrorResponse != null)
                    {
                        return(false);
                    }
                }

                if (!ConnectorIdURL.HasValue && !ConnectorIdBody.HasValue)
                {
                    ErrorResponse = "The connector identification is missing!";
                    return(false);
                }

                if (ConnectorIdURL.HasValue && ConnectorIdBody.HasValue && ConnectorIdURL.Value != ConnectorIdBody.Value)
                {
                    ErrorResponse = "The optional connector identification given within the JSON body does not match the one given in the URL!";
                    return(false);
                }

                #endregion

                #region Parse Standard            [mandatory]

                if (!JSON.ParseMandatoryEnum("standard",
                                             "connector standard",
                                             out ConnectorTypes Standard,
                                             out ErrorResponse))
                {
                    return(false);
                }

                #endregion

                #region Parse Format              [mandatory]

                if (!JSON.ParseMandatoryEnum("format",
                                             "connector format",
                                             out ConnectorFormats Format,
                                             out ErrorResponse))
                {
                    return(false);
                }

                #endregion

                #region Parse PowerType           [mandatory]

                if (!JSON.ParseMandatoryEnum("power_type",
                                             "power type",
                                             out PowerTypes PowerType,
                                             out ErrorResponse))
                {
                    return(false);
                }

                #endregion

                #region Parse MaxVoltage          [mandatory]

                if (!JSON.ParseMandatory("max_voltage",
                                         "max voltage",
                                         out UInt16 MaxVoltage,
                                         out ErrorResponse))
                {
                    return(false);
                }

                #endregion

                #region Parse MaxAmperage         [mandatory]

                if (!JSON.ParseMandatory("max_amperage",
                                         "max amperage",
                                         out UInt16 MaxAmperage,
                                         out ErrorResponse))
                {
                    return(false);
                }

                #endregion

                #region Parse MaxElectricPower    [optional]

                if (JSON.ParseOptional("max_electric_power",
                                       "max electric power",
                                       out UInt32? MaxElectricPower,
                                       out ErrorResponse))
                {
                    if (ErrorResponse != null)
                    {
                        return(false);
                    }
                }

                #endregion

                #region Parse TariffIds           [optional]

                if (JSON.ParseOptionalJSON("tariff_ids",
                                           "tariff identifications",
                                           Tariff_Id.TryParse,
                                           out IEnumerable <Tariff_Id> TariffIds,
                                           out ErrorResponse))
                {
                    if (ErrorResponse != null)
                    {
                        return(false);
                    }
                }

                #endregion

                #region TermsAndConditionsURL     [optional]

                if (JSON.ParseOptional("terms_and_conditions",
                                       "terms and conditions",
                                       URL.TryParse,
                                       out URL? TermsAndConditionsURL,
                                       out ErrorResponse))
                {
                    if (ErrorResponse != null)
                    {
                        return(false);
                    }
                }

                #endregion

                #region Parse LastUpdated         [mandatory]

                if (!JSON.ParseMandatory("last_updated",
                                         "last updated",
                                         out DateTime LastUpdated,
                                         out ErrorResponse))
                {
                    return(false);
                }

                #endregion


                Connector = new Connector(ConnectorIdBody ?? ConnectorIdURL.Value,
                                          Standard,
                                          Format,
                                          PowerType,
                                          MaxVoltage,
                                          MaxAmperage,

                                          MaxElectricPower,
                                          TariffIds?.Distinct(),
                                          TermsAndConditionsURL,

                                          LastUpdated);


                if (CustomConnectorParser != null)
                {
                    Connector = CustomConnectorParser(JSON,
                                                      Connector);
                }

                return(true);
            }
            catch (Exception e)
            {
                Connector     = default;
                ErrorResponse = "The given JSON representation of a connector is invalid: " + e.Message;
                return(false);
            }
        }