Skip to content
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

Fix File to String conversion on Windows #119

Merged
merged 4 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

name: CI
on:
pull_request:
Expand All @@ -14,6 +13,9 @@ jobs:
java: 8
jobtype: 1
- os: ubuntu-latest
java: 11
jobtype: 2
- os: windows-latest
java: 11
jobtype: 1
runs-on: ${{ matrix.os }}
Expand All @@ -34,4 +36,24 @@ jobs:
path: ~/.sbt
key: ${{ runner.os }}-sbt-cache-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }}
- name: Build and test
run: echo hello GitHub
run: |
case ${{ matrix.jobtype }} in
1)
sbt -v "+test;"
;;
2)
sbt -v "+test;"
SCALA_VERSION=2.12.12
sbt -v "++$SCALA_VERSION!" mimaReportBinaryIssues
sbt -v "++$SCALA_VERSION!" "benchmark/jmh:run -i 10 -wi 3 -f1 -t1"
;;
*)
echo unknown jobtype
exit 1
esac
rm -rf "$HOME/.ivy2/local" || true
find $HOME/Library/Caches/Coursier/v1 -name "ivydata-*.properties" -delete || true
find $HOME/.ivy2/cache -name "ivydata-*.properties" -delete || true
find $HOME/.cache/coursier/v1 -name "ivydata-*.properties" -delete || true
find $HOME/.sbt -name "*.lock" -delete || true
shell: bash
18 changes: 0 additions & 18 deletions .travis.yml

This file was deleted.

24 changes: 20 additions & 4 deletions core/src/main/scala/sjsonnew/JavaExtraFormats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package sjsonnew

import java.util.{ UUID, Optional }
import java.net.{ URI, URL }
import java.io.File
import java.math.{ BigInteger, BigDecimal => JBigDecimal }
import java.util.{ Locale, Optional, UUID }

trait JavaExtraFormats {
this: PrimitiveFormats with AdditionalFormats with IsoFormats =>
import JavaExtraFormats._

private[this] type JF[A] = JsonFormat[A] // simple alias for reduced verbosity

Expand All @@ -43,17 +44,27 @@ trait JavaExtraFormats {

implicit val fileStringIso: IsoString[File] = IsoString.iso[File](
(f: File) => {
if (f.isAbsolute) {
val p = f.getPath
if (p.startsWith(File.separatorChar.toString) && isWindows) {
if (p.startsWith("""\\""")) {
// supports \\laptop\My Documents\Some.doc on Windows
new URI(FileScheme, normalizeName(p), null).toASCIIString
}
else {
// supports /tmp on Windows
new URI(FileScheme, "", normalizeName(p), null).toASCIIString
}
} else if (f.isAbsolute) {
//not using f.toURI to avoid filesystem syscalls
//we use empty string as host to force file:// instead of just file:
new URI(FileScheme, "", normalizeName(slashify(f.getAbsolutePath)), null).toASCIIString
new URI(FileScheme, "", normalizeName(ensureHeadSlash(f.getAbsolutePath)), null).toASCIIString
} else {
new URI(null, normalizeName(f.getPath), null).toASCIIString
}
},
(s: String) => uriToFile(new URI(s)))

private[this] def slashify(name: String) = {
private[this] def ensureHeadSlash(name: String) = {
if(name.nonEmpty && name.head != File.separatorChar) File.separatorChar + name
else name
}
Expand Down Expand Up @@ -100,3 +111,8 @@ trait JavaExtraFormats {
}
}
}

object JavaExtraFormats {
private[sjsonnew] lazy val isWindows: Boolean =
System.getProperty("os.name").toLowerCase(Locale.ENGLISH).contains("windows")
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ package support.spray

import spray.json.{ JsValue, JsNumber, JsString, JsNull, JsTrue, JsFalse, JsObject }
import org.specs2.mutable._
import java.util.{ UUID, Optional }
import java.net.{ URI, URL }
import java.io.File
import java.util.{ Locale, Optional, UUID }

class JavaExtraFormatsSpec extends Specification with BasicJsonProtocol {
import JavaExtraFormats._

case class Person(name: Optional[String], value: Optional[Int])
implicit object PersonFormat extends JsonFormat[Person] {
def write[J](x: Person, builder: Builder[J]): Unit = {
Expand Down Expand Up @@ -91,6 +93,18 @@ class JavaExtraFormatsSpec extends Specification with BasicJsonProtocol {
"convert the JsString back to the relative path" in {
Converter.fromJsonUnsafe[File](JsString("src/main")) mustEqual f2
}
"convert an absolute path on Windows" in {
if (isWindows) Converter.toJsonUnsafe(new File("""C:\Documents and Settings\""")) mustEqual JsString("file:///C:/Documents%20and%20Settings")
else ok
}
"convert a relative path on Windows" in {
if (isWindows) Converter.toJsonUnsafe(new File("""..\My Documents\test""")) mustEqual JsString("../My%20Documents/test")
else ok
}
"convert a UNC path on Windows" in {
if (isWindows) Converter.toJsonUnsafe(new File("""\\laptop\My Documents\Some.doc""")) mustEqual JsString("file://laptop/My%20Documents/Some.doc")
else ok
}
}

"The optionalFormat" should {
Expand Down