-
Notifications
You must be signed in to change notification settings - Fork 369
Added Regex to remove JSON.NET '$ref' and '$id' properties #696
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I'd merge it, but I won't pretend I understand the regex 😊 Can you add some unit tests for |
|
Sure. I think I'll also rename it to |
|
The regex basically says "one or more characters, followed by either $ref or $id, wrapped in quotes, followed by one or more characters and a newline" 😄 |
|
If I run the following script using my ObjectDumper script pack, I'm sure it provides what you are after: This is just using settings on the JSON.Net JsonSerializerSettings object. If this is what you need then there's no need to perform additional regex after serialisation. |
|
Added two tests. Confirmed both failed without regex 😄 |
|
@paulbouwer How do you propose we set up our serializer to omit the |
|
Having read through the related discussions it appears that we do need to allow the serializer to add the $id and $ref properties for it to work correctly with circular references. |
Added Regex to remove JSON.NET '$ref' and '$id' properties
|
@khellang oh wow, good thinking! |
|
@khellang Sorry missed the circular reference part. Agreed @adamralph - that doesn't work without Do we lose too much context when removing before regex: after regex: |
|
@paulbouwer you may have a valid point here. It seems like $id and $ref aren't just noise. For circular refs, I guess there is no other way to represent them as JSON other than a $ref. For repeated objects, as in your address example, I would hope there is a way to tell JSON.NET to repeat the objects rather than $ref them. It's verbose and redundant information but I think it's far nicer to read. I wonder if @JamesNK can chip in here? Perhaps we should take another approach:
|
|
Oh I see the issue now, this is a good catch @paulbouwer. I feel like we are obsessing over this more than we need to. We talked about this before and decided NOT to pursue it because when we spoke to @JamesNK he described why there were needed. |
|
We should roll this commit back imo. |
|
@adamralph I really don't feel like this is worth a lot of effort. Let's just accept $id and $ref and move on |
|
OK talking with @adamralph I think we can reasonably go ahead and use regex to find all $id and all $ref. Then do a comparison to find which $id have no $ref and remove those. This would keep $id ONLY when $refs were present. |
|
IMO you should serialize directly to LINQ to JSON objects, navigate through the object tree and remove the $id properties that don't have a corresponding $ref, then ToString() to get the JSON and print it. Much better than using regex over strings. I think I suggested this when I got brought into a scriptcs discussion about $ref/$id for the 500th time! If you don't know how to do any of that I refer you to the documentation. Please just figure it out because it feels like ground hog day over here. |
|
Where I described what you should do: #553 (comment) And for bonus points, a comment where I described how to get rid of $id and $ref all together (which is what you originally had!) without a regex: #553 (comment) I think I am destined to live in a world where every 3 months scriptcs will alternate between wanting $id and $ref properties and not wanting them, and every time you'll ask me what to do. |
|
I agree with JamesNK on this. Let's use the API that JSON.NET provides. Going to all the way to a string and then regex'ing just doesn't make sense. Having said that, I don't think this is a huge priority. I've raised #700 for this. In the meantime we need to revert this PR. |
|
raised #701 to revert |
|
What if it is is a deep object graph? Wouldn't the regex cheaper then On Monday, May 19, 2014, James Newton-King notifications@github.com wrote:
|
|
You may have a point. I guess if anyone feels the inclination to pick up #700 then they can run some tests. |
|
@adamralph I'll pick up #700 - would like to fold some of those features into my ScriptCs.ObjectDumper script pack anyway. |
|
Awesome, thanks!
|

Fixes #553
Before:

After:
