Converting objects into JSON and using the StringBuilder
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
In my scenario I have list of object in C# code and need to be converted into JavaScript object. But there are certain condition, where the value of the object might be dynamic based on certain key.
I have a following method which will return string as JavaScript Object.
public string ItemToJson()
List < Item > itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
Here is the complete code of working console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String_Builder_Demo
class Program
public List<Item> GetItemList()
List<Item> items = new List<Item>
new Item() Key = "FirstName", Placeholder = "##FirstName##", Value="John" ,
new Item() Key = "LastName", Placeholder = "##LastName##", Value="Doe",
new Item() Key = "Email", Placeholder = "##Email##", Value="john.doe@domain.com " ,
new Item() Key = "Address", Placeholder = "##Address##", Value="Kathmandu" ,
new Item() Key = "Photo", Placeholder = "##Photo##", Value=""
;
return items;
public string GetImage()
return "http://via.placeholder.com/350x150";
static void Main(string args)
Program obj = new Program();
Console.WriteLine(obj.ItemToJson());
Console.ReadLine();
public string ItemToJson()
List<Item> itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
public class Item
public string Placeholder get; set;
public string Value get; set;
public string Key get; set;
The above mention code will return following string.
<script>
let Items =
FirstName:
placeholder: " ##FirstName## ",
value: " John "
,
LastName:
placeholder: " ##LastName## ",
value: " Doe "
,
Email:
placeholder: " ##Email## ",
value: " john.doe@domain.com "
,
Address:
placeholder: " ##Address## ",
value: " Kathmandu "
,
Photo:
placeholder: " ##Photo## ",
value: " http://via.placeholder.com/350x150 "
</script>
How can I optimize the above code and eliminate the if else
condition from ItemToJson()
method?
c# performance strings json
 |Â
show 2 more comments
up vote
2
down vote
favorite
In my scenario I have list of object in C# code and need to be converted into JavaScript object. But there are certain condition, where the value of the object might be dynamic based on certain key.
I have a following method which will return string as JavaScript Object.
public string ItemToJson()
List < Item > itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
Here is the complete code of working console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String_Builder_Demo
class Program
public List<Item> GetItemList()
List<Item> items = new List<Item>
new Item() Key = "FirstName", Placeholder = "##FirstName##", Value="John" ,
new Item() Key = "LastName", Placeholder = "##LastName##", Value="Doe",
new Item() Key = "Email", Placeholder = "##Email##", Value="john.doe@domain.com " ,
new Item() Key = "Address", Placeholder = "##Address##", Value="Kathmandu" ,
new Item() Key = "Photo", Placeholder = "##Photo##", Value=""
;
return items;
public string GetImage()
return "http://via.placeholder.com/350x150";
static void Main(string args)
Program obj = new Program();
Console.WriteLine(obj.ItemToJson());
Console.ReadLine();
public string ItemToJson()
List<Item> itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
public class Item
public string Placeholder get; set;
public string Value get; set;
public string Key get; set;
The above mention code will return following string.
<script>
let Items =
FirstName:
placeholder: " ##FirstName## ",
value: " John "
,
LastName:
placeholder: " ##LastName## ",
value: " Doe "
,
Email:
placeholder: " ##Email## ",
value: " john.doe@domain.com "
,
Address:
placeholder: " ##Address## ",
value: " Kathmandu "
,
Photo:
placeholder: " ##Photo## ",
value: " http://via.placeholder.com/350x150 "
</script>
How can I optimize the above code and eliminate the if else
condition from ItemToJson()
method?
c# performance strings json
Is there any particular reason you're doing this yourself instead using json.net?
â t3chb0t
Jul 17 at 6:21
@t3chb0t all values are in list except##Photo##
so I thing its impossible to use json.net
â Kiran Shahi
Jul 17 at 7:04
I'm pretty sure it's possible ;-) you probably just need a custom json-converter for this type.
â t3chb0t
Jul 17 at 7:06
@t3chb0t is there any way to removeif else
in this case ?
â Kiran Shahi
Jul 17 at 7:16
Why do you want to get rid of theif/else
statement? What's wrong with it?
â t3chb0t
Jul 17 at 7:22
 |Â
show 2 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
In my scenario I have list of object in C# code and need to be converted into JavaScript object. But there are certain condition, where the value of the object might be dynamic based on certain key.
I have a following method which will return string as JavaScript Object.
public string ItemToJson()
List < Item > itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
Here is the complete code of working console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String_Builder_Demo
class Program
public List<Item> GetItemList()
List<Item> items = new List<Item>
new Item() Key = "FirstName", Placeholder = "##FirstName##", Value="John" ,
new Item() Key = "LastName", Placeholder = "##LastName##", Value="Doe",
new Item() Key = "Email", Placeholder = "##Email##", Value="john.doe@domain.com " ,
new Item() Key = "Address", Placeholder = "##Address##", Value="Kathmandu" ,
new Item() Key = "Photo", Placeholder = "##Photo##", Value=""
;
return items;
public string GetImage()
return "http://via.placeholder.com/350x150";
static void Main(string args)
Program obj = new Program();
Console.WriteLine(obj.ItemToJson());
Console.ReadLine();
public string ItemToJson()
List<Item> itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
public class Item
public string Placeholder get; set;
public string Value get; set;
public string Key get; set;
The above mention code will return following string.
<script>
let Items =
FirstName:
placeholder: " ##FirstName## ",
value: " John "
,
LastName:
placeholder: " ##LastName## ",
value: " Doe "
,
Email:
placeholder: " ##Email## ",
value: " john.doe@domain.com "
,
Address:
placeholder: " ##Address## ",
value: " Kathmandu "
,
Photo:
placeholder: " ##Photo## ",
value: " http://via.placeholder.com/350x150 "
</script>
How can I optimize the above code and eliminate the if else
condition from ItemToJson()
method?
c# performance strings json
In my scenario I have list of object in C# code and need to be converted into JavaScript object. But there are certain condition, where the value of the object might be dynamic based on certain key.
I have a following method which will return string as JavaScript Object.
public string ItemToJson()
List < Item > itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
Here is the complete code of working console app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String_Builder_Demo
class Program
public List<Item> GetItemList()
List<Item> items = new List<Item>
new Item() Key = "FirstName", Placeholder = "##FirstName##", Value="John" ,
new Item() Key = "LastName", Placeholder = "##LastName##", Value="Doe",
new Item() Key = "Email", Placeholder = "##Email##", Value="john.doe@domain.com " ,
new Item() Key = "Address", Placeholder = "##Address##", Value="Kathmandu" ,
new Item() Key = "Photo", Placeholder = "##Photo##", Value=""
;
return items;
public string GetImage()
return "http://via.placeholder.com/350x150";
static void Main(string args)
Program obj = new Program();
Console.WriteLine(obj.ItemToJson());
Console.ReadLine();
public string ItemToJson()
List<Item> itemObj = GetItemList();
if (itemObj.Count > 0)
StringBuilder sbObj = new StringBuilder();
sbObj.Append("<script> let Items = ");
var len = itemObj.Count;
for (int i = 0; i < len; i++)
sbObj.Append(itemObj[i].Key);
sbObj.Append(": placeholder : " ");
sbObj.Append(itemObj[i].Placeholder);
sbObj.Append(" " , value : " ");
if (itemObj[i].Key == "Photo")
sbObj.Append(GetImage());
else
sbObj.Append(itemObj[i].Value);
sbObj.Append(" " ");
if (i < len - 1)
sbObj.Append(",");
sbObj.Append(" </script>");
return sbObj.ToString();
else
return string.Empty;
public class Item
public string Placeholder get; set;
public string Value get; set;
public string Key get; set;
The above mention code will return following string.
<script>
let Items =
FirstName:
placeholder: " ##FirstName## ",
value: " John "
,
LastName:
placeholder: " ##LastName## ",
value: " Doe "
,
Email:
placeholder: " ##Email## ",
value: " john.doe@domain.com "
,
Address:
placeholder: " ##Address## ",
value: " Kathmandu "
,
Photo:
placeholder: " ##Photo## ",
value: " http://via.placeholder.com/350x150 "
</script>
How can I optimize the above code and eliminate the if else
condition from ItemToJson()
method?
c# performance strings json
edited Jul 17 at 7:37
t3chb0t
31.8k54095
31.8k54095
asked Jul 17 at 5:26
Kiran Shahi
1536
1536
Is there any particular reason you're doing this yourself instead using json.net?
â t3chb0t
Jul 17 at 6:21
@t3chb0t all values are in list except##Photo##
so I thing its impossible to use json.net
â Kiran Shahi
Jul 17 at 7:04
I'm pretty sure it's possible ;-) you probably just need a custom json-converter for this type.
â t3chb0t
Jul 17 at 7:06
@t3chb0t is there any way to removeif else
in this case ?
â Kiran Shahi
Jul 17 at 7:16
Why do you want to get rid of theif/else
statement? What's wrong with it?
â t3chb0t
Jul 17 at 7:22
 |Â
show 2 more comments
Is there any particular reason you're doing this yourself instead using json.net?
â t3chb0t
Jul 17 at 6:21
@t3chb0t all values are in list except##Photo##
so I thing its impossible to use json.net
â Kiran Shahi
Jul 17 at 7:04
I'm pretty sure it's possible ;-) you probably just need a custom json-converter for this type.
â t3chb0t
Jul 17 at 7:06
@t3chb0t is there any way to removeif else
in this case ?
â Kiran Shahi
Jul 17 at 7:16
Why do you want to get rid of theif/else
statement? What's wrong with it?
â t3chb0t
Jul 17 at 7:22
Is there any particular reason you're doing this yourself instead using json.net?
â t3chb0t
Jul 17 at 6:21
Is there any particular reason you're doing this yourself instead using json.net?
â t3chb0t
Jul 17 at 6:21
@t3chb0t all values are in list except
##Photo##
so I thing its impossible to use json.netâ Kiran Shahi
Jul 17 at 7:04
@t3chb0t all values are in list except
##Photo##
so I thing its impossible to use json.netâ Kiran Shahi
Jul 17 at 7:04
I'm pretty sure it's possible ;-) you probably just need a custom json-converter for this type.
â t3chb0t
Jul 17 at 7:06
I'm pretty sure it's possible ;-) you probably just need a custom json-converter for this type.
â t3chb0t
Jul 17 at 7:06
@t3chb0t is there any way to remove
if else
in this case ?â Kiran Shahi
Jul 17 at 7:16
@t3chb0t is there any way to remove
if else
in this case ?â Kiran Shahi
Jul 17 at 7:16
Why do you want to get rid of the
if/else
statement? What's wrong with it?â t3chb0t
Jul 17 at 7:22
Why do you want to get rid of the
if/else
statement? What's wrong with it?â t3chb0t
Jul 17 at 7:22
 |Â
show 2 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
This is a bit off topic... but unless you are pushing a lot of data through this, StringBuilder isn't going to save you much.
Probably worth profiling to see. If you don't need it, then you can make the code pretty simple.
public string ItemToJson()
List<Item> itemObj = GetItemList();
if ( itemObj.Count <= 0 )
return string.Empty;
var parts = itemObj.Select (
( item ) =>
var val = item.Key == "Photo" ? GetImage () : item.Value;
return $"item.Key: placeholder : " item.Placeholder " , value : " val " ";
);
var json = string.Join ( "," , parts);
return $"<script> let Items = json </script>";
You have very unusual code formatting... spaces everywhere :-o
â t3chb0t
Jul 17 at 16:09
1
Many years ago, monks found copying books really hard, so they started adding spaces between the words to make it easier to keep track of where you were upto. This caught on as it makes reading the text easlier. One day I hope developers will have the same epiphany. :P
â Nigel Thorne
Jul 18 at 5:05
@NigelThorne: hah, I do the same, but not as much as you :P (spaces before(
in method calls, and method definitions)
â apocalypse
Jul 18 at 15:54
@apocalypse I was playing with Resharper settings... :) I'm not sure I'll keep all of them going forward but I do think in general developers don't use enough whitespace.
â Nigel Thorne
Jul 18 at 23:31
add a comment |Â
up vote
1
down vote
I'd recommend using a library for something like this. Formatting JSON manually difficult and error prone. Json.NET is nice and straight forward in this case.
public string ItemToJson()
var result = new JObject();
foreach (var property in GetItemList())
result.Add(property.Key, new JObject
["placeholder"] = property.Placeholder,
["value"] = property.Key != "Photo"
? property.Value
: "http://via.placeholder.com/350x150"
);
return $"<script>let Items = JsonConvert.SerializeObject(result);</script>";
add a comment |Â
up vote
0
down vote
Had to put it here as it was getting too big for comment.
You definitely need to evaluate that condition based on code you have posted. So, the if/else construct is fine. Alternate options will need you to split the list into separate ones which will be more costly operations.
If I really have to nitpick, I would change the key
datatype to int
or add another property in Item
class itself which is a bool
. Perhaps something of is this sort:
class Item
private string _key;
public string Key
get
return _key;
set
_key = value;
IsPhoto = (string.Compare(value, "Photo", true) == 0);
public bool IsPhoto
get;
set;
This will remove the string comparison in loop and move it to object creation/update action.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This is a bit off topic... but unless you are pushing a lot of data through this, StringBuilder isn't going to save you much.
Probably worth profiling to see. If you don't need it, then you can make the code pretty simple.
public string ItemToJson()
List<Item> itemObj = GetItemList();
if ( itemObj.Count <= 0 )
return string.Empty;
var parts = itemObj.Select (
( item ) =>
var val = item.Key == "Photo" ? GetImage () : item.Value;
return $"item.Key: placeholder : " item.Placeholder " , value : " val " ";
);
var json = string.Join ( "," , parts);
return $"<script> let Items = json </script>";
You have very unusual code formatting... spaces everywhere :-o
â t3chb0t
Jul 17 at 16:09
1
Many years ago, monks found copying books really hard, so they started adding spaces between the words to make it easier to keep track of where you were upto. This caught on as it makes reading the text easlier. One day I hope developers will have the same epiphany. :P
â Nigel Thorne
Jul 18 at 5:05
@NigelThorne: hah, I do the same, but not as much as you :P (spaces before(
in method calls, and method definitions)
â apocalypse
Jul 18 at 15:54
@apocalypse I was playing with Resharper settings... :) I'm not sure I'll keep all of them going forward but I do think in general developers don't use enough whitespace.
â Nigel Thorne
Jul 18 at 23:31
add a comment |Â
up vote
1
down vote
This is a bit off topic... but unless you are pushing a lot of data through this, StringBuilder isn't going to save you much.
Probably worth profiling to see. If you don't need it, then you can make the code pretty simple.
public string ItemToJson()
List<Item> itemObj = GetItemList();
if ( itemObj.Count <= 0 )
return string.Empty;
var parts = itemObj.Select (
( item ) =>
var val = item.Key == "Photo" ? GetImage () : item.Value;
return $"item.Key: placeholder : " item.Placeholder " , value : " val " ";
);
var json = string.Join ( "," , parts);
return $"<script> let Items = json </script>";
You have very unusual code formatting... spaces everywhere :-o
â t3chb0t
Jul 17 at 16:09
1
Many years ago, monks found copying books really hard, so they started adding spaces between the words to make it easier to keep track of where you were upto. This caught on as it makes reading the text easlier. One day I hope developers will have the same epiphany. :P
â Nigel Thorne
Jul 18 at 5:05
@NigelThorne: hah, I do the same, but not as much as you :P (spaces before(
in method calls, and method definitions)
â apocalypse
Jul 18 at 15:54
@apocalypse I was playing with Resharper settings... :) I'm not sure I'll keep all of them going forward but I do think in general developers don't use enough whitespace.
â Nigel Thorne
Jul 18 at 23:31
add a comment |Â
up vote
1
down vote
up vote
1
down vote
This is a bit off topic... but unless you are pushing a lot of data through this, StringBuilder isn't going to save you much.
Probably worth profiling to see. If you don't need it, then you can make the code pretty simple.
public string ItemToJson()
List<Item> itemObj = GetItemList();
if ( itemObj.Count <= 0 )
return string.Empty;
var parts = itemObj.Select (
( item ) =>
var val = item.Key == "Photo" ? GetImage () : item.Value;
return $"item.Key: placeholder : " item.Placeholder " , value : " val " ";
);
var json = string.Join ( "," , parts);
return $"<script> let Items = json </script>";
This is a bit off topic... but unless you are pushing a lot of data through this, StringBuilder isn't going to save you much.
Probably worth profiling to see. If you don't need it, then you can make the code pretty simple.
public string ItemToJson()
List<Item> itemObj = GetItemList();
if ( itemObj.Count <= 0 )
return string.Empty;
var parts = itemObj.Select (
( item ) =>
var val = item.Key == "Photo" ? GetImage () : item.Value;
return $"item.Key: placeholder : " item.Placeholder " , value : " val " ";
);
var json = string.Join ( "," , parts);
return $"<script> let Items = json </script>";
answered Jul 17 at 13:45
Nigel Thorne
30328
30328
You have very unusual code formatting... spaces everywhere :-o
â t3chb0t
Jul 17 at 16:09
1
Many years ago, monks found copying books really hard, so they started adding spaces between the words to make it easier to keep track of where you were upto. This caught on as it makes reading the text easlier. One day I hope developers will have the same epiphany. :P
â Nigel Thorne
Jul 18 at 5:05
@NigelThorne: hah, I do the same, but not as much as you :P (spaces before(
in method calls, and method definitions)
â apocalypse
Jul 18 at 15:54
@apocalypse I was playing with Resharper settings... :) I'm not sure I'll keep all of them going forward but I do think in general developers don't use enough whitespace.
â Nigel Thorne
Jul 18 at 23:31
add a comment |Â
You have very unusual code formatting... spaces everywhere :-o
â t3chb0t
Jul 17 at 16:09
1
Many years ago, monks found copying books really hard, so they started adding spaces between the words to make it easier to keep track of where you were upto. This caught on as it makes reading the text easlier. One day I hope developers will have the same epiphany. :P
â Nigel Thorne
Jul 18 at 5:05
@NigelThorne: hah, I do the same, but not as much as you :P (spaces before(
in method calls, and method definitions)
â apocalypse
Jul 18 at 15:54
@apocalypse I was playing with Resharper settings... :) I'm not sure I'll keep all of them going forward but I do think in general developers don't use enough whitespace.
â Nigel Thorne
Jul 18 at 23:31
You have very unusual code formatting... spaces everywhere :-o
â t3chb0t
Jul 17 at 16:09
You have very unusual code formatting... spaces everywhere :-o
â t3chb0t
Jul 17 at 16:09
1
1
Many years ago, monks found copying books really hard, so they started adding spaces between the words to make it easier to keep track of where you were upto. This caught on as it makes reading the text easlier. One day I hope developers will have the same epiphany. :P
â Nigel Thorne
Jul 18 at 5:05
Many years ago, monks found copying books really hard, so they started adding spaces between the words to make it easier to keep track of where you were upto. This caught on as it makes reading the text easlier. One day I hope developers will have the same epiphany. :P
â Nigel Thorne
Jul 18 at 5:05
@NigelThorne: hah, I do the same, but not as much as you :P (spaces before
(
in method calls, and method definitions)â apocalypse
Jul 18 at 15:54
@NigelThorne: hah, I do the same, but not as much as you :P (spaces before
(
in method calls, and method definitions)â apocalypse
Jul 18 at 15:54
@apocalypse I was playing with Resharper settings... :) I'm not sure I'll keep all of them going forward but I do think in general developers don't use enough whitespace.
â Nigel Thorne
Jul 18 at 23:31
@apocalypse I was playing with Resharper settings... :) I'm not sure I'll keep all of them going forward but I do think in general developers don't use enough whitespace.
â Nigel Thorne
Jul 18 at 23:31
add a comment |Â
up vote
1
down vote
I'd recommend using a library for something like this. Formatting JSON manually difficult and error prone. Json.NET is nice and straight forward in this case.
public string ItemToJson()
var result = new JObject();
foreach (var property in GetItemList())
result.Add(property.Key, new JObject
["placeholder"] = property.Placeholder,
["value"] = property.Key != "Photo"
? property.Value
: "http://via.placeholder.com/350x150"
);
return $"<script>let Items = JsonConvert.SerializeObject(result);</script>";
add a comment |Â
up vote
1
down vote
I'd recommend using a library for something like this. Formatting JSON manually difficult and error prone. Json.NET is nice and straight forward in this case.
public string ItemToJson()
var result = new JObject();
foreach (var property in GetItemList())
result.Add(property.Key, new JObject
["placeholder"] = property.Placeholder,
["value"] = property.Key != "Photo"
? property.Value
: "http://via.placeholder.com/350x150"
);
return $"<script>let Items = JsonConvert.SerializeObject(result);</script>";
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I'd recommend using a library for something like this. Formatting JSON manually difficult and error prone. Json.NET is nice and straight forward in this case.
public string ItemToJson()
var result = new JObject();
foreach (var property in GetItemList())
result.Add(property.Key, new JObject
["placeholder"] = property.Placeholder,
["value"] = property.Key != "Photo"
? property.Value
: "http://via.placeholder.com/350x150"
);
return $"<script>let Items = JsonConvert.SerializeObject(result);</script>";
I'd recommend using a library for something like this. Formatting JSON manually difficult and error prone. Json.NET is nice and straight forward in this case.
public string ItemToJson()
var result = new JObject();
foreach (var property in GetItemList())
result.Add(property.Key, new JObject
["placeholder"] = property.Placeholder,
["value"] = property.Key != "Photo"
? property.Value
: "http://via.placeholder.com/350x150"
);
return $"<script>let Items = JsonConvert.SerializeObject(result);</script>";
answered Jul 23 at 17:22
Matt Cole
763410
763410
add a comment |Â
add a comment |Â
up vote
0
down vote
Had to put it here as it was getting too big for comment.
You definitely need to evaluate that condition based on code you have posted. So, the if/else construct is fine. Alternate options will need you to split the list into separate ones which will be more costly operations.
If I really have to nitpick, I would change the key
datatype to int
or add another property in Item
class itself which is a bool
. Perhaps something of is this sort:
class Item
private string _key;
public string Key
get
return _key;
set
_key = value;
IsPhoto = (string.Compare(value, "Photo", true) == 0);
public bool IsPhoto
get;
set;
This will remove the string comparison in loop and move it to object creation/update action.
add a comment |Â
up vote
0
down vote
Had to put it here as it was getting too big for comment.
You definitely need to evaluate that condition based on code you have posted. So, the if/else construct is fine. Alternate options will need you to split the list into separate ones which will be more costly operations.
If I really have to nitpick, I would change the key
datatype to int
or add another property in Item
class itself which is a bool
. Perhaps something of is this sort:
class Item
private string _key;
public string Key
get
return _key;
set
_key = value;
IsPhoto = (string.Compare(value, "Photo", true) == 0);
public bool IsPhoto
get;
set;
This will remove the string comparison in loop and move it to object creation/update action.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Had to put it here as it was getting too big for comment.
You definitely need to evaluate that condition based on code you have posted. So, the if/else construct is fine. Alternate options will need you to split the list into separate ones which will be more costly operations.
If I really have to nitpick, I would change the key
datatype to int
or add another property in Item
class itself which is a bool
. Perhaps something of is this sort:
class Item
private string _key;
public string Key
get
return _key;
set
_key = value;
IsPhoto = (string.Compare(value, "Photo", true) == 0);
public bool IsPhoto
get;
set;
This will remove the string comparison in loop and move it to object creation/update action.
Had to put it here as it was getting too big for comment.
You definitely need to evaluate that condition based on code you have posted. So, the if/else construct is fine. Alternate options will need you to split the list into separate ones which will be more costly operations.
If I really have to nitpick, I would change the key
datatype to int
or add another property in Item
class itself which is a bool
. Perhaps something of is this sort:
class Item
private string _key;
public string Key
get
return _key;
set
_key = value;
IsPhoto = (string.Compare(value, "Photo", true) == 0);
public bool IsPhoto
get;
set;
This will remove the string comparison in loop and move it to object creation/update action.
answered Jul 17 at 11:38
danish
1392
1392
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199651%2fconverting-objects-into-json-and-using-the-stringbuilder%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Is there any particular reason you're doing this yourself instead using json.net?
â t3chb0t
Jul 17 at 6:21
@t3chb0t all values are in list except
##Photo##
so I thing its impossible to use json.netâ Kiran Shahi
Jul 17 at 7:04
I'm pretty sure it's possible ;-) you probably just need a custom json-converter for this type.
â t3chb0t
Jul 17 at 7:06
@t3chb0t is there any way to remove
if else
in this case ?â Kiran Shahi
Jul 17 at 7:16
Why do you want to get rid of the
if/else
statement? What's wrong with it?â t3chb0t
Jul 17 at 7:22