//// =========================================================================================================== //// Methods //// =========================================================================================================== public static bool TryCreateFromJsonString( string jsonString, out BridgeMessageDeserializer deserializer, out IServiceCommandResponse errorResponse) { Param.VerifyString(jsonString, nameof(jsonString)); var valueSet = new Dictionary <string, object>(); try { // Try to parse the JSON. var jsonObject = JObject.Parse(jsonString); // Populate a value set from the parsed JSON. foreach (KeyValuePair <string, JToken> pair in jsonObject) { valueSet.Add(pair.Key, ((JValue)pair.Value).Value); } } catch (Exception e) { deserializer = null; errorResponse = ServiceCommandResponse.CreateError(ServiceCommandName.Unknown, e); return(false); } return(TryCreateFromValueSet(valueSet, out deserializer, out errorResponse)); }
private static bool TryGetCommandName( IDictionary <string, object> valueSet, out ServiceCommandName commandName, out IServiceCommandResponse errorResponse) { if (!valueSet.TryGetValue(ParamName.CommandName.ToString(), out object rawValue)) { errorResponse = ServiceCommandResponse.CreateError( ServiceCommandName.Unknown, ServiceErrorInfo.MissingRequiredMessageValue(ParamName.CommandName)); commandName = ServiceCommandName.Unknown; return(false); } if (!Enum.TryParse(rawValue.ToString(), out commandName)) { errorResponse = ServiceCommandResponse.CreateError( ServiceCommandName.Unknown, ServiceErrorInfo.WrongMessageValueType( ParamName.CommandName, rawValue.GetType(), typeof(ServiceCommandName))); commandName = ServiceCommandName.Unknown; return(false); } errorResponse = null; return(true); }
private bool GetRequiredValue(ParamName paramName, out object value) { if (ValueSet.TryGetValue(paramName.ToString(), out value)) { return(true); } LastError = ServiceCommandResponse.CreateError( CommandName, ServiceErrorInfo.MissingRequiredMessageValue(paramName)); return(false); }
private bool TryConvertEnumValue <T>(ParamName paramName, object rawValue, out T value) where T : struct { try { value = (T)Enum.Parse(typeof(T), rawValue.ToString()); return(true); } catch (Exception e) when(e is ArgumentException || e is OverflowException) { LastError = ServiceCommandResponse.CreateError( CommandName, ServiceErrorInfo.WrongMessageValueType(paramName, rawValue.GetType(), typeof(T))); value = default(T); return(false); } }
public string GetStringValue(ParamName paramName) { if (!GetRequiredValue(paramName, out object rawValue)) { return(null); } if (rawValue is string value) { return(value); } LastError = ServiceCommandResponse.CreateError( CommandName, ServiceErrorInfo.WrongMessageValueType(paramName, rawValue.GetType(), typeof(string))); return(null); }
public int GetIntValue(ParamName paramName) { if (!GetRequiredValue(paramName, out object rawValue)) { return(0); } if (rawValue is int value) { return(value); } LastError = ServiceCommandResponse.CreateError( CommandName, ServiceErrorInfo.WrongMessageValueType(paramName, rawValue.GetType(), typeof(int))); return(0); }