internal static unsafe bool TryParse_t_data_type_display(string s, out t_data_type value) { t_data_type_remove_nullable value_type_value; JArray jarray; IF("$t_data_type->is_bool()"); { IF("$t_data_type->is_nullable()"); { if (string.IsNullOrEmpty(s) || string.Compare(s, "null", ignoreCase: true) == 0) { value = default(t_data_type); return(true); } } END(); double double_val; if (t_data_type_remove_nullable.TryParse(s, out value_type_value)) { value = value_type_value; return(true); } else if (double.TryParse(s, out double_val)) { LITERAL_OUTPUT("value = (double_val != 0);"); MUTE(); value = value_type_value; MUTE_END(); return(true); } else { value = default(t_data_type); return(false); } } ELIF("$t_data_type->is_datetime()"); { IF("$t_data_type->is_nullable()"); { if (string.IsNullOrEmpty(s) || string.Compare(s, "null", ignoreCase: true) == 0) { value = default(t_data_type); return(true); } } END(); if (t_data_type_remove_nullable.TryParse(s, null, System.Globalization.DateTimeStyles.RoundtripKind, out value_type_value)) { value = value_type_value; return(true); } //special case for non-standard date time strings if (s.EndsWith(" UTC", StringComparison.Ordinal) && t_data_type_remove_nullable.TryParse(s.Substring(0, s.Length - 4) + 'Z', null, System.Globalization.DateTimeStyles.RoundtripKind, out value_type_value)) { //replace " UTC" with "Z" value = value_type_value; return(true); } value = default(t_data_type); return(false); } ELIF("$t_data_type->is_nullable() /* and has built-in TryParse */"); if (string.IsNullOrEmpty(s) || string.Compare(s, "null", ignoreCase: true) == 0) { value = default(t_data_type); return(true); } else if (t_data_type_remove_nullable.TryParse(s, out value_type_value)) { value = value_type_value; return(true); } else { value = default(t_data_type); return(false); } ELIF("$t_data_type->is_array()"); try { MUTE(); //Just to make csc happy with the meta template. value = default(t_data_type); int t_array_iterator_length = 1; string element = ""; MUTE_END(); jarray = JArray.Parse(s); //There are some situations that our template system doesn't work... //So we use LITERAL_OUTPUT to output something hard to express LITERAL_OUTPUT("value = new "); META_OUTPUT("data_type_get_array_type_with_size_string($t_data_type)"); LITERAL_OUTPUT(";"); META_OUTPUT("$t_data_type_array_element_type"); LITERAL_OUTPUT(" element;"); int t_array_iterator_prefix_offset = 0; FOREACH(); for (int t_array_iterator_string = 0; t_array_iterator_string < t_array_iterator_length; ++t_array_iterator_string) /*END*/ { IF("data_type_need_external_parser($t_data_type_array_element_type)"); { if (!ExternalParser.TryParse_t_data_type_array_element_type((string)jarray[t_array_iterator_prefix_offset++], out element)) { continue; } value[/*FOREACH(",")*/ t_array_iterator_string /*END*/] = element; } ELIF("$t_data_type_array_element_type->is_string()"); { value[/*FOREACH(",")*/ t_array_iterator_string /*END*/] = (string)jarray[t_array_iterator_prefix_offset++]; } ELSE(); { if (!t_data_type_array_element_type.TryParse((string)jarray[t_array_iterator_prefix_offset++], out element)) { continue; } value[/*FOREACH(",")*/ t_array_iterator_string /*END*/] = element; } END(); } return(true); } catch { value = default(t_data_type); return(false); } ELIF("$t_data_type->is_list()"); try { value = new t_data_type(); jarray = JArray.Parse(s); foreach (var jarray_element in jarray) { t_data_type_list_element_type element; IF("data_type_need_external_parser($t_data_type->listElementType)"); if (!ExternalParser.TryParse_t_data_type_list_element_type_display((string)jarray_element, out element)) { continue; //TODO what to do if we fail on an item? //Same situation applies for arrays also. //throw new Exception("Cannot parse \""+capture.Value+"\" into [META_OUTPUT($t_data_type->listElementType)]."); } value.Add(element); ELIF("$t_data_type->listElementType->is_string()");//Assign unescaped json string. value.Add((string)jarray_element); ELSE(); if (!t_data_type_list_element_type.TryParse((string)jarray_element, out element)) { continue; //throw new Exception("Cannot parse \""+capture.Value+"\" into [META_OUTPUT($t_data_type->listElementType)]."); } value.Add(element); END(); } return(true); } catch { value = default(t_data_type); return(false); } END(); }
t_field_type_2 ITypeConverter <t_field_type_2> .ConvertFrom_t_field_type_display(t_field_type value) { META_VAR("TypeConversionAction", "conversion_action", "$t_field_type_2->get_type_conversion_action($t_field_type)"); IF("%conversion_action == TypeConversionAction::TC_ASSIGN"); return((t_field_type_2)value); ELIF("%conversion_action == TypeConversionAction::TC_TOSTRING"); return(Serializer.ToString(value)); ELIF("%conversion_action == TypeConversionAction::TC_PARSESTRING"); { #region String parse t_field_type_2 intermediate_result; bool conversion_success; IF("data_type_need_external_parser($t_field_type_2)"); { conversion_success = ExternalParser.TryParse_t_field_type_2_display(value, out intermediate_result); } ELSE(); { conversion_success = t_field_type_2.TryParse(value, out intermediate_result); } END(); if (!conversion_success) { IF("$t_field_type_2->is_list()"); try { t_field_type_2_element_type element = TypeConverter <t_field_type_2_element_type> .ConvertFrom_t_field_type_display(value); intermediate_result = new t_field_type_2(); intermediate_result.Add(element); } catch { throw new ArgumentException("Cannot parse \"" + value + "\" into either 't_field_type_2' or 't_field_type_2_element_type'."); } ELSE(); Throw.cannot_parse(value, "t_field_type_2"); END(); } return(intermediate_result); #endregion } ELIF("%conversion_action == TypeConversionAction::TC_TOBOOL"); return(value != 0); ELIF("%conversion_action == TypeConversionAction::TC_CONVERTLIST"); { t_field_type_2 intermediate_result = new t_field_type_2(); foreach (var element in value) { intermediate_result.Add(TypeConverter <t_field_type_2_element_type> .ConvertFrom_t_field_type_element_type_display(element)); } return(intermediate_result); } ELIF("%conversion_action == TypeConversionAction::TC_WRAPINLIST"); { t_field_type_2 intermediate_result = new t_field_type_2(); intermediate_result.Add(TypeConverter <t_field_type_2_element_type> .ConvertFrom_t_field_type_display(value)); return(intermediate_result); } ELIF("%conversion_action == TypeConversionAction::TC_ARRAYTOLIST"); return(TypeConverter <t_field_type> .Enumerate_t_field_type_2_element_type_display(value).ToList()); ELIF("%conversion_action == TypeConversionAction::TC_EXTRACTNULLABLE"); return(TypeConverter <t_field_type_2> .ConvertFrom_t_field_type_remove_nullable(value.Value)); ELSE(); throw new InvalidCastException("Invalid cast from '[META_OUTPUT($t_field_type)]' to '[META_OUTPUT($t_field_type_2)]'."); END(); }