-
-
Notifications
You must be signed in to change notification settings - Fork 105
v2 Data Sources
axunonb edited this page Feb 6, 2022
·
1 revision
Data type | What does it look for? | Examples |
---|---|---|
Any object | Properties, Fields, or parameterless methods | Smart.Format("{Prop.ToString}", new{ Prop = 999 }) |
Dictionary | Keys | Smart.Format("{SomeKey}", new Dictionary<string, object>(){ { "SomeKey", 999 } } ) |
Dynamics | Works just like Dictionary | Smart.Format("{Prop}", (dynamic)(new { Prop = 999 })) |
JObject (Newtonsoft JSON) |
Finds child elements by name | Smart.Format("{Name}", JObject.Parse("{ 'Name':'John'}")) |
JsonElement (System.Text.Json) |
Finds child elements by name | Smart.Format("{Name}", JsonDocument.Parse("{ \"Name\":\"John\"}").RootElement) |
ValueTuple | Find elements of objects in ValueTuple by their member name | Smart.Format("{Name1} {Name1}", (obj1, obj2)); |
XML | Finds child elements by name | Smart.Format("{NodeName}", someXElement) |
Default | Looks up arguments by index | Smart.Format("{0}", 999) |
JSON also comes in handy when processing data in a web API application where the argument submitted from the browser to the controller is JSON. Another scenario is working with queries from SQL Server:
SELECT 'John' AS [FirstName], 'Doe' AS [LastName], 32 AS [Age]
FOR JSON PATH, ROOT('Father')
You can parse the query result into a JObject
(Newtonsoft.Json) or JsonElement
(System.Text.Json) and give it to SmartFormat as an argument. JObject
and JElement
may contain arrays for the ListFormatter.
Note:
- The examples below use Dictionaries for simplicity. In the same manner other types than Dictionaries can be used as data sources.
- Use
ValueTuple
s (introduced in v2.5) instead ofSmartObjects
because they are less expensive.
With ValueTuples
- all objects used for Smart.Format can be collected in one place as the first argument
- the format string can be written like each object would be the first argument of Smart.Format
- there is no need to bother from which argument a value should come from
- child ValueTuples will be flattened
Example:
var dict1 = new Dictionary<string, string> { {"dict1key", "dict1 Value"} };
var dict2 = new Dictionary<string, string> { { "dict2key", "dict2 Value" } };
Smart.Format("[{dict1key}] [{dict2key}] and more...", (dict1, dict2));
// result:
// "[dict1 Value] [dict2 Value] and more"
Note:
- The examples below use Dictionaries for simplicity. In the same manner other types than Dictionaries can be used as data sources.
- Use
ValueTuple
s instead ofSmartObjects
because they are less expensive.
With SmartObjects
- all objects used for Smart.Format can be collected in one place as the first argument
- the format string can be written like each object would be the first argument of Smart.Format
- there is no need to bother from which argument a value should come from
Example:
var d1 = new Dictionary<string,string> { {"myKey", "myValue"} };
var nl = new Dictionary<string,string> { {"2ndKey", "my2ndValue" } };
var smartObj = new SmartObjects();
smartObj.AddRange(new object[] {d1, nl});
Smart.Format("{myKey} {2ndKey} and more...", smartObj);
// result:
// "myValue my2ndValue and more"
Like with string.Format
it is possible to use several data sources as parameters to SmartFormat. The conecept however is a bit different:
var dict1 = new Dictionary<string, string>() { {"Name", "John"} };
var dict2 = new Dictionary<string, string>() { { "City", "Washington" } };
var result = Smart.Format("First dictionary: {0:{Name}}, second dictionary: {1:{City}}", dict1, dict2);
// alternative notation:
var result = Smart.Format("First dictionary: {0.Name}, second dictionary: {1.City}", dict1, dict2);
// result:
// "First dictionary: John, second dictionary: Washington"
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License