Renaming JSON fields

217 views
Skip to first unread message

Уразалинов Даулет

unread,
Oct 21, 2009, 9:50:47 AM10/21/09
to Jayrock
Hi, all!

I'm have to rename all fields in my JSON object. I used "JSON
for .NET" and in this library was very nice solve of my problem. Maybe
it's very naive, but it quite works.

private string SerializeAndRenameFields(string fieldsPrefix, object
objectForSerialization) {
JavaScriptSerializer serializer = new JavaScriptSerializer();
var s = serializer.Serialize
(objectForSerialization).ReplaceQuotesToFranceQuotes();

// Parsing JSON into object
JsonTextParser jsonTextParser = new JsonTextParser();
JsonObject obj = jsonTextParser.Parse(s);

// Adding prefix to fields name
foreach (var field in obj as JsonObjectCollection) {
field.Name = fieldsPrefix + field.Name;
}

return obj.ToString();
}

All works fine, but this library isn't in development at now and
unfortunately has some bugs with arrays.

And I'm looking for some way to solve my problem.

Can I use Jayrock for this purpose?

Atif Aziz

unread,
Oct 21, 2009, 1:23:56 PM10/21/09
to jay...@googlegroups.com
Are you taking an object, serializing it, then parsing it back and prefixing each field and then taking the final JSON? In other words, if you have an class like this:
 
public class Person {
  public string FirstName { get; set; }
  public string LastName { get; set; }
}
 
You want that the serialized JSON looks like this if the prefix is "p:" ?
 
{
  "p:FirstName", "John",
  "p:LastName", "Doe"
}
 
Let me know if I got you write and then we can talk about how to make it work with Jayrock.
 
- Atif
2009/10/21 Уразалинов Даулет <d.uraz...@gmail.com>
Message has been deleted

Уразалинов Даулет

unread,
Oct 21, 2009, 11:44:22 PM10/21/09
to Jayrock
Yes, you are right!

I want to add prefixes too. But I also want to work with fields in
more complex way. Change it, filter fields by name and etc.

On 21 окт, 23:23, Atif Aziz <aziza...@gmail.com> wrote:
> Are you taking an object, serializing it, then parsing it back and prefixing
> each field and then taking the final JSON? In other words, if you have an
> class like this:
>
> public class Person {
>    public string FirstName { get; set; }
>   public string LastName { get; set; }
>
> }
>
> You want that the serialized JSON looks like this if the prefix is "p:" ?
>
> {
>   "p:FirstName", "John",
>   "p:LastName", "Doe"
>
> }
>
> Let me know if I got you write and then we can talk about how to make it
> work with Jayrock.
>
> - Atif
> 2009/10/21 Уразалинов Даулет <d.urazali...@gmail.com>

Atif Aziz

unread,
Oct 24, 2009, 6:12:30 AM10/24/09
to jay...@googlegroups.com
I've attached a small sample that shows how you can manipulate field names by applying an arbitrary function to them. The way it accomplishes this is by implementing a FilteringJsonWriter that intercepts everything written down to a base JsonWriter.
 
var w = new JsonTextWriter(Console.Out) { PrettyPrint = true };
var fw = new FilteringJsonWriter(w,
             name => "env::" + name.ToLowerInvariant());
var env = Environment.GetEnvironmentVariables();
JsonConvert.Export(env, fw);
Here you can see the usage. First a normal JsonTextWriter is created to write to standard output. Next, it is decorated with a FilteringJsonWriter where a function is given to add "env::" to every member name while also transforming to lowercase. Finally, the environment variables are exported. Since this GetEnvironmentVariables returns a dictionary, it is written out as a JSON object. As the object is exported, the filtering writer gets called to write out the real JSON, which in turn calls back the transformation function for member names.
 
The FilteringJsonWriter has a trivial implementation. It holds on to the base writer and transformation function during construction. Then during calls to WriteMember, it calls back out function on the member name before passing it along to the base writer:
 
public override void WriteMember(string name) {
  _writer.WriteMember(_memberNameFunc(name));
}
 
This is one way to accomplish it and works at a lower level than the conversion infrastructure of Jayrock. Depending on what you're trying to accomplish overall, this might be what you're looking for or there may be other more effective ways at arriving at the same result.
 
- Atif

2009/10/22 Уразалинов Даулет <d.uraz...@gmail.com>
FilteringJsonWriterDemo.cs

Уразалинов Даулет

unread,
Oct 25, 2009, 5:32:58 AM10/25/09
to Jayrock
It's cool! Thank you!

And can you show some example to filter fields for renaming. For
example if i want to rename only root of the JSON object or root and 1
level children?

On 24 окт, 16:12, Atif Aziz <aziza...@gmail.com> wrote:
> I've attached a small sample that shows how you can manipulate field names
> by applying an arbitrary function to them. The way it accomplishes this is
> by implementing a *FilteringJsonWriter* that intercepts everything written
> down to a base *JsonWriter*.
>
> var w = new JsonTextWriter(Console.Out) { PrettyPrint = true };
> var fw = new FilteringJsonWriter(w,
>              name => "env::" + name.ToLowerInvariant());
> var env = Environment.GetEnvironmentVariables();
> JsonConvert.Export(env, fw);
> Here you can see the usage. First a normal JsonTextWriter is created to
> write to standard output. Next, it is decorated with a
> *FilteringJsonWriter*where a function is given to add "env::" to every
> member name while also
> transforming to lowercase. Finally, the environment variables are exported.
> Since this *GetEnvironmentVariables* returns a dictionary, it is written out
> as a JSON object. As the object is exported, the filtering writer gets
> called to write out the real JSON, which in turn calls back the
> transformation function for member names.
>
> The FilteringJsonWriter has a trivial implementation. It holds on to the
> base writer and transformation function during construction. Then during
> calls to WriteMember, it calls back out function on the member name before
> passing it along to the base writer:
>
> public override void WriteMember(string name) {
>   _writer.WriteMember(_memberNameFunc(name));
>
> }
>
> This is one way to accomplish it and works at a lower level than the
> conversion infrastructure of Jayrock. Depending on what you're trying to
> accomplish overall, this might be what you're looking for or there may be
> other more effective ways at arriving at the same result.
>
> - Atif
>
> 2009/10/22 Уразалинов Даулет <d.urazali...@gmail.com>
>  FilteringJsonWriterDemo.cs
> 2KПросмотретьЗагрузить

Atif Aziz

unread,
Oct 25, 2009, 6:18:27 AM10/25/09
to jay...@googlegroups.com
For example if i want to rename only root of the JSON object or root and 1
level children?
You determine the "root-ness" or level by looking at the JsonWriter.Depth property and then run some logic like filtering based on that.

- Atif
2009/10/25 Уразалинов Даулет <d.uraz...@gmail.com>
Reply all
Reply to author
Forward
0 new messages