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ПросмотретьЗагрузить