-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValues.cs
More file actions
114 lines (95 loc) · 3.58 KB
/
Values.cs
File metadata and controls
114 lines (95 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
namespace JSONPredicate
{
internal static class Values
{
public static object Parse(string value, Type targetType)
{
value = value.Trim();
// Handle IN operator values (parentheses format)
if (value.StartsWith("(") && value.EndsWith(")"))
{
var items = ParseInOperatorValues(value.Substring(1, value.Length - 2));
return items;
}
return ParseSingleValue(value, targetType);
}
private static IEnumerable<object> ParseInOperatorValues(string value)
{
var items = new List<string>();
var currentItem = "";
var inQuotes = false;
var quoteChar = '\0';
for (int i = 0; i < value.Length; i++)
{
var c = value[i];
if (!inQuotes && (c == '\'' || c == '"' || c == '`'))
{
inQuotes = true;
quoteChar = c;
currentItem += c;
continue;
}
if (inQuotes && c == quoteChar)
{
inQuotes = false;
quoteChar = '\0';
currentItem += c;
continue;
}
if (!inQuotes && c == ',')
{
items.Add(currentItem.Trim());
currentItem = "";
continue;
}
currentItem += c;
}
if (!string.IsNullOrWhiteSpace(currentItem))
{
items.Add(currentItem.Trim());
}
// Parse each item based on its content
var parsedItems = new List<object>();
foreach (var item in items)
{
var trimmedItem = item.Trim();
// Remove quotes if present
if ((trimmedItem.StartsWith("'") && trimmedItem.EndsWith("'")) ||
(trimmedItem.StartsWith("`") && trimmedItem.EndsWith("`")) ||
(trimmedItem.StartsWith("\"") && trimmedItem.EndsWith("\"")))
{
trimmedItem = trimmedItem.Substring(1, trimmedItem.Length - 2);
}
parsedItems.Add(trimmedItem);
}
return parsedItems;
}
private static object ParseSingleValue(string value, Type targetType)
{
// Remove quotes if present
if ((value.StartsWith("'") && value.EndsWith("'")) ||
(value.StartsWith("`") && value.EndsWith("`")) ||
(value.StartsWith("\"") && value.EndsWith("\"")))
{
value = value.Substring(1, value.Length - 2);
}
// Handle DateTime
if (targetType == typeof(DateTime) || targetType == typeof(DateTime?) ||
(targetType == null && DataTypes.IsLikelyDateTime(value)))
{
return DataTypes.ParseDateTime(value);
}
if (targetType == typeof(string) || targetType == null)
return value;
if (targetType == typeof(int) || targetType == typeof(int?))
return int.Parse(value);
if (targetType == typeof(bool) || targetType == typeof(bool?))
return bool.Parse(value);
if (targetType == typeof(double) || targetType == typeof(double?))
return double.Parse(value);
return value;
}
}
}