Skip to content

Commit

Permalink
Merge pull request #86 from eed3si9n/wip/url
Browse files Browse the repository at this point in the history
Encode POSIX file path to URI using u3 (file:///)
  • Loading branch information
eed3si9n committed Dec 11, 2017
2 parents 162447e + f6471b7 commit 7b91375
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
7 changes: 6 additions & 1 deletion build.sbt
@@ -1,4 +1,5 @@
import Dependencies._
import com.typesafe.tools.mima.core._

val scala210 = "2.10.6"
val scala211 = "2.11.11"
Expand Down Expand Up @@ -63,7 +64,11 @@ lazy val core = project
name := "sjson new core",
libraryDependencies ++= testDependencies,
scalacOptions ++= Seq("-feature", "-language:_", "-unchecked", "-deprecation", "-encoding", "utf8"),
mimaSettings
mimaSettings,
mimaBinaryIssueFilters ++= Seq(
// private[this] final val
ProblemFilters.exclude[ReversedMissingMethodProblem]("sjsonnew.JavaExtraFormats.sjsonnew$JavaExtraFormats$$fileScheme")
)
)

def support(n: String) =
Expand Down
30 changes: 29 additions & 1 deletion core/src/main/scala/sjsonnew/JavaExtraFormats.scala
Expand Up @@ -39,8 +39,36 @@ trait JavaExtraFormats {
implicit val urlStringIso: IsoString[URL] = IsoString.iso[URL](
_.toURI.toASCIIString, (s: String) => (new URI(s)).toURL)

private[this] final val fileScheme = "file"

implicit val fileStringIso: IsoString[File] = IsoString.iso[File](
_.toURI.toASCIIString, (s: String) => new File(new URI(s)))
(f: File) => {
if (f.isAbsolute) {
f.toPath.toUri.toASCIIString
} else {
new URI(fileScheme, normalizeName(f.getPath), null).toASCIIString
}
},
(s: String) => uriToFile(new URI(s)))

private[this] def normalizeName(name: String) = {
val sep = File.separatorChar
if (sep == '/') name else name.replace(sep, '/')
}

private[this] def uriToFile(uri: URI): File = {
assert(
uri.getScheme == fileScheme,
"Expected protocol to be '" + fileScheme + "' in URI " + uri
)
val part = uri.getSchemeSpecificPart
Option(uri.getAuthority) match {
case None if part startsWith "/" => new File(uri)
case _ =>
if (!(part startsWith "/") && (part contains ":")) new File("//" + part)
else new File(part)
}
}

implicit def optionalFormat[A :JF]: JF[Optional[A]] = new OptionalFormat[A]
final class OptionalFormat[A :JF] extends JF[Optional[A]] {
Expand Down
Expand Up @@ -77,11 +77,18 @@ class JavaExtraFormatsSpec extends Specification with BasicJsonProtocol {

"The fileStringIso" should {
val f = new File("/tmp")
val f2 = new File("src")
"convert a File to JsString" in {
Converter.toJsonUnsafe(f) mustEqual JsString("file:/tmp/")
Converter.toJsonUnsafe(f) mustEqual JsString("file:///tmp/")
}
"convert the JsString back to the URI" in {
Converter.fromJsonUnsafe[File](JsString("file:/tmp/")) mustEqual f
"convert a relative path to JsString" in {
Converter.toJsonUnsafe(f2) mustEqual JsString("file:src")
}
"convert the JsString back to the File" in {
Converter.fromJsonUnsafe[File](JsString("file:///tmp/")) mustEqual f
}
"convert the JsString back to the relative path" in {
Converter.fromJsonUnsafe[File](JsString("file:src")) mustEqual f2
}
}

Expand Down

0 comments on commit 7b91375

Please sign in to comment.