-
Notifications
You must be signed in to change notification settings - Fork 90
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
Correct json for nested objects #126
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version=1.1.1 | ||
sbt.version=1.1.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,16 +63,20 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO | |
def toJsonLine: Seq[String] = | ||
if (options contains BuildInfoOption.ToJson) | ||
List( | ||
""" val toJson: String = toMap.map{ i => | ||
| def quote(x:Any) : String = "\"" + x + "\"" | ||
| val key : String = quote(i._1) | ||
| val value : String = i._2 match { | ||
| case elem : Seq[_] => elem.map(quote).mkString("[", ",", "]") | ||
| case elem : Option[_] => elem.map(quote).getOrElse("null") | ||
| case elem => quote(elem) | ||
""" private def quote(x: Any): String = "\"" + x + "\"" | ||
| | ||
| private def toJsonValue(value: Any): String = { | ||
| value match { | ||
| case elem: Seq[_] => elem.map(toJsonValue).mkString("[", ",", "]") | ||
| case elem: Option[_] => elem.map(toJsonValue).orNull | ||
| case elem: Map[String, Any] => elem.map { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Minor - feature creep): We could also match on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I think we can do it |
||
| case (k, v) => quote(k) + ":" + toJsonValue(v) | ||
| }.mkString("{", ", ", "}") | ||
| case other => quote(other) | ||
| } | ||
| s"$key : $value" | ||
| }.mkString("{", ", ", "}")""".stripMargin) | ||
| } | ||
| | ||
| val toJson: String = toJsonValue(toMap)""".stripMargin) | ||
else Nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should return
null
. I think returning"null"
was correct. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree, we try to convert to correct json type and
null
is not"null"
.For example, we parse
BuildInfo.toJson
and instead of getting for some valueOption[String]
we getString
.https://www.json.org/
"A value can be a string in double quotes, or a number, or true or false or null, or an object or an array."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean. We are converting to the correct JSON type, but the string representation of that type.
In that case string representation of JSON null is
"null"
, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At final we get something like that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that works because (on line 73) the
null
you're returning right now is passed toString#+
And
"commit" + ":" + null
=="commit:null"
.So it's kind of "accidentally doing the right thing". But I'm fairly certain we be returning "null" here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, I get
null
on line 71 where we work with Option.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, that's the code the produces the
null
from the option and gets returned by the call totoJsonValue
.The code calling that
toJsonValue
is the code at line 73, which is consuming the map you described in the PR description: #126 (comment)It's only because your passing it to
String#+
that it works.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, maybe I missed something. Can you explain by example, why you want to return "quoted null"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to return the string representation of JSON null.
Here, these examples might help:
toJsonValue(Some(123))
should return"123"
toJsonValue(Some("abc"))
should return"\"abc\""
(quoted string)toJsonValue(None)
should return"null"
Another addition we could do is make
toJsonValue(null)
return"null"
.Sorry, the fact that the code is recursive and there's multiple levels of strings makes this hard to communicate. 😄