forked from sbt/librarymanagement
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e219c2c
commit a07d336
Showing
4 changed files
with
89 additions
and
16 deletions.
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
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
32 changes: 32 additions & 0 deletions
32
core/src/main/scala/sbt/librarymanagement/ScmInfoFormats.scala
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// format: off | ||
package sbt.librarymanagement | ||
|
||
import _root_.sjsonnew.{Builder, JsonFormat, Unbuilder, deserializationError} | ||
|
||
trait ScmInfoFormats { self: sjsonnew.BasicJsonProtocol => | ||
implicit lazy val ScmInfoFormat: JsonFormat[sbt.librarymanagement.ScmInfo] = new JsonFormat[sbt.librarymanagement.ScmInfo] { | ||
override def read[J](__jsOpt: Option[J], unbuilder: Unbuilder[J]): sbt.librarymanagement.ScmInfo = { | ||
__jsOpt match { | ||
case Some(__js) => | ||
unbuilder.beginObject(__js) | ||
// for backwards compatibility, we need to read the browseUrl field instead of browseUri | ||
val browseUrl = unbuilder.readField[java.net.URI]("browseUrl") | ||
val connection = unbuilder.readField[String]("connection") | ||
val devConnection = unbuilder.readField[Option[String]]("devConnection") | ||
unbuilder.endObject() | ||
sbt.librarymanagement.ScmInfo(browseUrl, connection, devConnection) | ||
case None => | ||
deserializationError("Expected JsObject but found None") | ||
} | ||
} | ||
override def write[J](obj: sbt.librarymanagement.ScmInfo, builder: Builder[J]): Unit = { | ||
builder.beginObject() | ||
// for backwards compatibility, we need to write the browseUrl field instead of browseUri | ||
builder.addField("browseUrl", obj.browseUri) | ||
builder.addField("connection", obj.connection) | ||
builder.addField("devConnection", obj.devConnection) | ||
builder.endObject() | ||
} | ||
} | ||
} | ||
// format: on |
24 changes: 24 additions & 0 deletions
24
core/src/test/scala/sbt/librarymanagement/ScmInfoTest.scala
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package sbt.librarymanagement | ||
|
||
import sjsonnew.support.scalajson.unsafe.{ CompactPrinter, Converter, Parser } | ||
|
||
import java.net.URI | ||
|
||
object ScmInfoTest extends verify.BasicTestSuite { | ||
|
||
test("it should format itself into JSON") { | ||
import LibraryManagementCodec._ | ||
val json = Converter.toJson(ScmInfo(new URI("/"), "connection", "devconnection")).get | ||
assert(CompactPrinter(json) == expectedJson) | ||
} | ||
|
||
test("it should thaw back from JSON") { | ||
import LibraryManagementCodec._ | ||
val json = Parser.parseUnsafe(expectedJson) | ||
val m = Converter.fromJsonUnsafe[ScmInfo](json) | ||
assert(m == ScmInfo(new URI("/"), "connection", "devconnection")) | ||
} | ||
|
||
def expectedJson = | ||
"""{"browseUrl":"/","connection":"connection","devConnection":"devconnection"}""" | ||
} |